MongoDB Insert Document
Syntax to insert a document into the collection:
db.collection_name.insert()
MongoDB Insert Document using insert() Example
Here we are inserting a document into the collection named “employeeDB”. The field “location” in the example below is an array that holds the several key-value pairs.
db.employee.insert(
{
"Employeeid" : 5,
"dept_id" : 3,
"EmployeeName" : "Mahesh",
"location" : [ { "city" : "Chennai", "state" : "Karnataka" } ]
}
)
You should see a successful write message like this:
WriteResult({ "nInserted" : 1 })
The insert() method creates the collection if it doesn’t exist but if the collection is present then it inserts the document into it
Verification:
You can also verify whether the document is successfully inserted by typing following command:
db.collection_name.find()
In the above example, we inserted the document in the collection named employeeDB so the command should be:
db.employee.find()
{ "_id" : ObjectId("5b3b65a36728a1b55ecec85c"),
"Employeeid" : 5,
"dept_id" : 3,
"EmployeeName" : "Mahesh",
"location" : [ { "city" : "Chennai", "state" : "Karnataka" } ]
}
MongoDB Example: Insert Multiple Documents in collection
To insert multiple documents in collection, we define an array of documents and later we use the insert() method on the array variable as shown in the example below. Here we are inserting three documents in the collection named “employee”.
This command will insert the data in “employee” collection, if the collection is not present then it will create the collection and insert these documents.
var employeesColl =[
{
"Employeeid" : 1,
"dept_id" : 2,
"EmployeeName" : "Mohan",
"location" : [ { "city" : "Trichy", "state" : "TN" } ]
},
{
"Employeeid" : 2,
"dept_id" : 2,
"EmployeeName" : "James",
"location" : [ { "city" : "Chennai", "state" : "TN" } ]
}];
db.employee.insert(employeesColl);
You would see this output:
db.employee.insert(employeesColl);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
As you can see that it shows number 2 in front of nInserted. this means that the 2 documents have been inserted by this command.
db.employee.find()
{ "_id" : ObjectId("5b3b70706728a1b55ecec85e"), "Employeeid" : 1, "dept_id" : 2, "EmployeeName" : "Mohan", "location" : [ { "city" : "Trichy", "state" : "TN" } ] }
{ "_id" : ObjectId("5b3b70706728a1b55ecec85f"), "Employeeid" : 2, "dept_id" : 2, "EmployeeName" : "James", "location" : [ { "city" : "Chennai", "state" : "TN" } ] }
You can print the output data in a JSON format so that you can read it easily. To print the data in JSON format run the command db.collection_name.find().forEach(printjson)
So in our case the command would be this:
db.employee.find().forEach(printjson)
First we have printed the documents using normal find() method and then we printed the documents of same collection using JSON format. The documents in JSON format are neat and easy to read.
db.employee.find().forEach(printjson)
{
"_id" : ObjectId("5b3b70096728a1b55ecec85d"),
"Employeeid" : 5,
"dept_id" : 3,
"EmployeeName" : "Mahesh",
"location" : [
{
"city" : "Chennai",
"state" : "Karnataka"
}
]
}
{
"_id" : ObjectId("5b3b70706728a1b55ecec85e"),
"Employeeid" : 1,
"dept_id" : 2,
"EmployeeName" : "Mohan",
"location" : [
{
"city" : "Trichy",
"state" : "TN"
}
]
}
{
"_id" : ObjectId("5b3b70706728a1b55ecec85f"),
"Employeeid" : 2,
"dept_id" : 2,
"EmployeeName" : "James",
"location" : [
{
"city" : "Chennai",
"state" : "TN"
}
]
}