MongoDB - Insert Document
MongoDB - Insert Document
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insert(document)
Example
> db.users.insert({
... })
WriteResult({ "nInserted" : 1 })
>
Here mycol is our collection name, as created in the previous chapter. If the collection doesn't
exist in the database, then MongoDB will create this collection and then insert a document into it.
In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a unique
ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are
divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
You can also pass an array of documents into the insert() method as shown below:.
> db.createCollection("post")
> db.post.insert([
{
url: "http://www.tutorialspoint.com",
https://www.tutorialspoint.com/mongodb/mongodb_insert_document.htm 1/3
7/31/22, 7:55 PM MongoDB - Insert Document
likes: 100
},
url: "http://www.tutorialspoint.com",
likes: 20,
comments: [
user:"user1",
like: 0
])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
To insert the document you can use db.post.save(document) also. If you don't specify _id in the
document then save() method will work same as insert() method. If you specify _id then it will
replace whole data of document containing _id as specified in save() method.
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insertOne(document)
Example
Following example creates a new collection named empDetails and inserts a document using the
insertOne() method.
> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
First_Name: "Radhika",
https://www.tutorialspoint.com/mongodb/mongodb_insert_document.htm 2/3
7/31/22, 7:55 PM MongoDB - Insert Document
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "[email protected]",
phone: "9848022338"
})
"acknowledged" : true,
"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
>
Example
Following example inserts three different documents into the empDetails collection using the
insertMany() method.
> db.empDetails.insertMany(
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "[email protected]",
phone: "9000012345"
},
First_Name: "Rachel",
Last_Name: "Christopher",
Date_Of_Birth: "1990-02-16",
e_mail: "[email protected]",
phone: "9000054321"
},
First_Name: "Fathima",
Last_Name: "Sheik",
Date_Of_Birth: "1990-02-16",
e_mail: "[email protected]",
phone: "9000054321"
"acknowledged" : true,
"insertedIds" : [
ObjectId("5dd631f270fb13eec3963bed"),
ObjectId("5dd631f270fb13eec3963bee"),
ObjectId("5dd631f270fb13eec3963bef")
>
https://www.tutorialspoint.com/mongodb/mongodb_insert_document.htm 3/3