MongoDB Insert Document - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
MongoDB Insert Document

MongoDB Insert Document

MongoDB Insert Document

n this tutorial, we will see how to insert a document into the collection. We will understand this with the help of multiple examples.
Syntax to insert a document into the collection:
db.collection_name.insert()
Lets take an example to understand this .

MongoDB Insert Document using insert() Example

Here we are inserting a document into the collection named “mycareerrepublic”. The field “course” in the example below is an array that holds the several key-value pairs.
db.mycareerrepublic.insert(  
{
name
: "Chaitanya",
age
: 30,
email
: "admin@mycareerrepublic.com",
course
: [ { name: "MongoDB", duration: 7 }, { name: "Java", duration: 30 } ]
}
)
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 “beginnersbook” so the command should be:
> db.mycareerrepublic.find()
{ "_id" : ObjectId("59bce797668dcce02aaa6fec"), "name" : "Chaitanya", "age" : 30,
"email" : "admin@mycareerrepublic.com", "course" : [ { "name" : "MongoDB",
"duration" : 7 }, { "name" : "Java", "duration" : 30 } ] }

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 “students”. This command will insert the data in “students” collection, if the collection is not present then it will create the collection and insert these documents.
var beginners =
[
{
"StudentId" : 1001,
"StudentName" : "Steve",
"age": 30
},
{
"StudentId" : 1002,
"StudentName" : "Negan",
"age": 42
},
{
"StudentId" : 3333,
"StudentName" : "Rick",
"age": 35
},
];
db
.students.insert(beginners);
You would see this output:
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
As you can see that it shows number 3 in front of nInserted. this means that the 3 documents have been inserted by this command.
To verify that the documents are there in collection. Run this command:
db.students.find()
Do you know? 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.students.find().forEach(printjson)
In the screenshot below, you can see the difference. 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.

We can also insert multiple documents using the New Bulk API introduced in MongoDB 2.6. We will learn that in the Bulk API tutorial.

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.