Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing MongoDB Chapter 3

Creating documents


There are different queries to create a document in MongoDB, such as insertOne(),


insertMany(), and insert().


insertOne()

The insertOne() command adds a single document to our collection. For example:


> db.users.insertOne(
{
name: "Brooke",
email: "[email protected]",
address: 'Kathmandu'
}
)

This command takes only a single parameter, which is an object, and we can pass the field


names and values we want for our users collection. When we run the preceding code in


our Terminal inside the Mongo shell, we should get the following output:


It returns the _id of the document that just got created. We have successfully created a


collection and a document in the users collection.

Free download pdf