MongoDB Insert Document

db.myCollection.insert() and db.myCollection.save() methods use to insert document in collection mongoDB.


Syntax



db.Collection_Name.insert(document)



Example

Suppose we have an employee collection ‘EMP’ and want to insert document like Name, Salary, Project, and date of joining (DOJ). We can insert a document using following query script.


>use Test
--switched to db Test

>db.EMP.insert(
                        {
                                "Name":"Dilip",
                                "Salary":50000,
                                "Project":"Codeafri Mongodb",
                                "DOJ":ISODate("2014-12-01T20:19:55.782Z")
                        }
                    )


If EMP collection no exists in the database it will create a new collection EMP otherwise in the same collection. If document inserted successfully it will return and output as below.

Output


Inserted 1 record(s) in 1ms


If any error generated then it will give error as output.


Find inserted document


Executing following command you can find the inserted documents in the collection.


db.EMP.find()



Output



{
    "_id" : ObjectId("584a5b82309356d26bef1769"),
    "Name" : "Dilip",
    "Salary" : 50000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}


Here _id is auto generated 12 bytes hexadecimal unique number. If you did not specify the _id parameter, mongoDB assign a unique Object_Id for inserted document.


Insert Multiple Documents


If you want to insert multiple documents in a single query, then we have to pass an array of documents in the insert() method, see the following example.


db.EMP.insert
(
   [
             {
             "Name":"Dilip",
             "Salary":50000,
             "Project":"Codeafri Mongodb",
             "DOJ":ISODate("2014-12-01T20:19:55.782Z")
             },
             {
             "Name":"Vipul",
             "Salary":30000,
             "Project":"Codeafri UI",
             "DOJ":ISODate("2015-11-01T20:19:55.782Z")
             },
             {
             "Name":"Ashish",
             "Salary":100000,
             "Project":"Codeafri Project architect",
             "DOJ":ISODate("2013-10-01T20:19:55.782Z")
             }                          
   ]
)




Find inserted document


Now you can see the inserted document execution the following command script.


db.EMP.find()



Output



/* 1 */
{
    "_id" : ObjectId("584a5cd7309356d26bef176a"),
    "Name" : "Dilip",
    "Salary" : 50000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}

/* 2 */
{
    "_id" : ObjectId("584a61b7309356d26bef176b"),
    "Name" : "Dilip",
    "Salary" : 50000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}

/* 3 */
{
    "_id" : ObjectId("584a61b7309356d26bef176c"),
    "Name" : "Vipul",
    "Salary" : 30000.0,
    "Project" : "Codeafri UI",
    "DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}

/* 4 */
{
    "_id" : ObjectId("584a61b7309356d26bef176d"),
    "Name" : "Ashish",
    "Salary" : 100000.0,
    "Project" : "Codeafri Project architect",
    "DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}



db.myCollection.save() is also use to insert the document in a collection, these methods add new document through called upsert.


Upsert is an operation that performs as either replaces an existing document or an insert a new document if doesn’t match to any existing document.