MongoDB – Insert Document

MongoDB – Insert Document

In this chapter, we will learn how to insert document in MongoDB collection.

The insert() Method

To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.

Syntax

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insert(document)

Example

> db.phone.insert({
...         "_id" : "ac3",
...         "name" : "AC3 Phone",
...         "brand" : "ACME",
...         "type" : "phone",
...         "price" : 200,
...         "rating" : 3.8,
...         "warranty_years" : 1,
...         "available" : true
... })
WriteResult({ "nInserted" : 1 })
>

Here phone 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.phones.insert([{
... ...         "_id" : "ac3",
... ...         "name" : "AC3 Phone",
... ...         "brand" : "ACME",
... ...         "type" : "phone",
... ...         "price" : 200,
... ...         "rating" : 3.8,
... ...         "warranty_years" : 1,
... ...         "available" : true
... ... },{
... ...         "_id" : "ac7",
... ...         "name" : "AC7 Phone",
... ...         "brand" : "ACME",
... ...         "type" : "phone",
... ...         "price" : 320,
... ...         "rating" : 4,
... ...         "warranty_years" : 1,
... ...         "available" : false
... ... },{
...         "_id" : "ac9",
...         "name" : "AC9 Phone",
...         "brand" : "ACME",
...         "type" : "phone",
...         "price" : 320,
...         "rating" : 4,
...         "warranty_years" : 1,
...         "available" : false
... }])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>

To insert the document you can use db.phones.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.

The insertOne() method

If you need to insert only one document into a collection you can use this method.

Syntax

The basic syntax of insert() command is as follows −

>db.COLLECTION_NAME.insertOne(document)

Example

Following example creates a new collection named student and inserts a document using the insertOne() method.

> db.createCollection("student")
{ "ok" : 1 }
> db.createCollection("student")
{ "ok" : 1 }
> db.student.insertOne({"_id":0,"name":"aimee Zank",class:10})
{ "acknowledged" : true, "insertedId" : 0 }

The insertMany() method

You can insert multiple documents using the insertMany() method. To this method you need to pass an array of documents.

Example

Following example inserts three different documents into the student collection using the insertMany() method.

> db.student.insertMany([{"_id":0,"name":"aimee Zank","scores":[{"score":1.463179736705023,"type":"exam"},{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]},{"_id":1,"name":"Aurelia Menendez","scores":[{"score":60.06045071030959,"type":"exam"},{"score":52.79790691903873,"type":"quiz"},{"score":71.76133439165544,"type":"homework"}]},{"_id":2,"name":"Corliss Zuk","scores":[{"score":67.03077096065002,"type":"exam"},{"score":6.301851677835235,"type":"quiz"},{"score":66.28344683278382,"type":"homework"}]}])
{ "acknowledged" : true, "insertedIds" : [ 0, 1, 2 ] }
>
MongoDB – Datatypes (Prev Lesson)
(Next Lesson) MongoDB – Query Document