We can insert new documents by using the insertOne or insertMany methods as follows:
- Insert a single document: We can insert a new document like this:
> db.<collection>.insertOne(<document>)
Let's insert one document with the following code:
db.user.insertOne(
{
name: "Alexandre",
age: 30,
slug: "alexandre",
role: "admin",
status: "ok"
}
)
You should get a similar result to this:
{
"acknowledged" : true,
"insertedId" : ObjectId("5ca...")
}
- Insert multiple documents: We can insert multiple new documents like this:
> db.<collection>.insertMany([<document>,<document>,<document>,...])
Let's insert two documents with the following code:
> db.user.insertMany([
{
name: "Pooya",
age: 25,
slug: "pooya",
role: "admin",
status: "ok"
},
{
name: "Sébastien...