MongoDB Insert Document

This post will help us to learn How to add document in the MongoDB collection.

MongoDB has `insert()` and `save()` method to add a document into collection.

Syntax:


db.COLLECTION_NAME.insert(document)

Insert Single record:

Example:

> use learnmongodb switched to db learnmongodb > db.books.insert({
'title':'MongoDB',
'description':'Learn MongoDB with NoSQL',
'version':'4.x'
})
WriteResult({ "nInserted" : 1 })
[OR]

> db.books.insertOne({
'title':'MongoDB',
'description':'Learn MongoDB with NoSQL',
'version':'4.x'
})
{
"acknowledged" : true,
"insertedId" : ObjectId("5b8e859670e393ef22ee63ed")
}
>

insertOne() - Return Value: It returns the _id of the document inserted into the database.

insert() or insertOne() method will create a new collection If It does not exist in selected MongoDB.

To retrieve the inserted documents from the collection, We need to use find() method.


> db.books.find()
{ "_id" : ObjectId("5b8e859670e393ef22ee63ed"), "title" : "MongoDB", "description" : "Learn MongoDB with NoSQL", "version" : "4.x" }
> db.books.find().pretty()
{
"_id" : ObjectId("5b8e859670e393ef22ee63ed"),
"title" : "MongoDB",
"description" : "Learn MongoDB with NoSQL",
"version" : "4.x"
}
>

In above sample, We have learned to insert a document and displayed data from collection using find() method.

Insert Multiple records:

To insert multiple documents in a single query, We have to pass an array of documents.

Example:


> db.books.insert([ {
'title':'PHP',
'description':'Learn PHP with MongoDB',
'version':'4.x'
},
{
'title':'JAVA',
'description':'Learn JAVA with MongoDB',
'version':'4.x'
},
{
'title':'C#',
'description':'Learn C# with MongoDB',
'version':'4.x'
},
])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
[OR]

> db.books.insertMany([ {
'title':'PHP',
'description':'Learn PHP with MongoDB',
'version':'4.x'
},
{
'title':'JAVA',
'description':'Learn JAVA with MongoDB',
'version':'4.x'
},
{
'title':'C#',
'description':'Learn C# with MongoDB',
'version':'4.x'
},
])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5b8e877270e393ef22ee63ee"),
ObjectId("5b8e877270e393ef22ee63ef"),
ObjectId("5b8e877270e393ef22ee63f0")
]
}
>

insertMany() - Return Value: It returns the _ids of the documents inserted into the database.

To retrieve the inserted records,

> db.books.find().pretty() {
"_id" : ObjectId("5b8e859670e393ef22ee63ed"),
"title" : "MongoDB",
"description" : "Learn MongoDB with NoSQL",
"version" : "4.x"
}
{
"_id" : ObjectId("5b8e877270e393ef22ee63ee"),
"title" : "PHP",
"description" : "Learn PHP with MongoDB",
"version" : "4.x"
}
{
"_id" : ObjectId("5b8e877270e393ef22ee63ef"),
"title" : "JAVA",
"description" : "Learn JAVA with MongoDB",
"version" : "4.x"
}
{
"_id" : ObjectId("5b8e877270e393ef22ee63f0"),
"title" : "C#",
"description" : "Learn C# with MongoDB",
"version" : "4.x"
}
>

_id Field:

If a document doesn't have a `_id` field, MongoDB will add new field `_id` in the document and assign a new unique object id to that field before inserting that record in the database.

If a document is having `_id`, It must be unique. Otherwise, we will get a duplicate key error.

Insert Document with `_id`:

> db.books.insert({
"_id" : 123456789,
"title" : "NodeJS",
"description" : "Learn JAVA with MongoDB",
"version" : "4.x"
})
WriteResult({ "nInserted" : 1 })
> db.books.find().pretty()
{
"_id" : 123456789,
"title" : "NodeJS",
"description" : "Learn JAVA with MongoDB",
"version" : "4.x"
}
>

Add Document using Save() method:

To insert a document, We can use save() method instead of insert() method.

- If you do not specify `_id` in the document, It will add a new document like insert() method.

- If you specify `_id` in the document, It will replace that document data.

Post a Comment

0 Comments