eolas/zk/Adding_documents_to_a_collection.md

49 lines
1 KiB
Markdown
Raw Normal View History

2022-08-30 14:00:04 +01:00
---
2022-11-19 17:30:05 +00:00
tags: [mongo-db, node-js, mongoose]
2022-08-30 14:00:04 +01:00
---
# Adding documents to a collection
We have our database (`playground`) and collection (`courses`) established. We
now need to add documents to our collection. We will do this via a function
since this will be an asynchronous process:
2022-08-30 14:00:04 +01:00
```js
const pythonCourse = new Course({
name: "Python Course",
author: "Terry Ogleton",
tags: ["python", "backend"],
isPublished: true,
});
async function addCourseDocToDb(courseDocument) {
try {
const result = await courseDocument.save();
console.log(result);
} catch (err) {
console.error(err.message);
}
}
addCourseDocToDb(nodeCourse);
```
When we run this, we call the `save` method on the Mongoose schema. We will then
have the Mongo document outputted to the console:
2022-08-30 14:00:04 +01:00
```
{
name: 'Python Course',
author: 'Terry Ogleton',
tags: [ 'python' ],
isPublished: true,
_id: new ObjectId("62f4ac989d2fec2f01596b9b"),
date: 2022-08-11T07:15:36.978Z,
__v: 0
}
```
This will also be reflected in Compass:
2024-02-16 16:14:01 +00:00
![](/img/mongo-doc-added.png)