We can pass array of
documents to the db.collection.insert () method to insert the multiple
documents into the collection.
To create an array of documents, need to define the variable with some
name and assign the array of documents to the created variable and then insert
into Mongodb.Syntax: var somename = [ array of documents ];
db.collection.insert(somename);
Example: var Student_data =
[
{
Student_name : "Raghav",
Department
: "Computer Science and Engg",
Percentage
: "76%",
Address : { Street:"Burkit Road", City :
"chennai",State:"Tamil Nadu" },
Gender : "Male"
},
{
Student_name : "Asthosh",
Department
: "Computer Science and Engg",
Percentage
: "72%",
Address : { Street:"Gandhi Road",City :
"Hyderbad",State:"Telangana" },
Gender : "Male"
},
{
Student_name : "john",
Department
: "Computer Science and Engg",
Percentage
: "65%",
Address : { Street:"nicholus Road",City :
"Chennai",State:"Tamil Nadu" },
Gender : "Male"
},
];
db.studentDetails.insert(Student_data)
After the successful
insertion of data the resultant shows as below
BulkWriteResult
( {
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted"
: 0,
"nMatched"
: 0,
"nModified"
: 0,
"nRemoved"
: 0,
"upserted"
: [ ]
})