Use db.collection.insert() method to insert new document to MongoDB collection. You don’t need to create the collection first. Insert method will automatically create collection if not exists.
Syntax
db.COLLECTION_NAME.insert(document)
db.users.insert({
"id": 1001,
"user_name": "A",
"name": [
{"first_name": "A"},
{"middle_name": ""},
{"last_name": "D"}
],
"email": "ad@aodba",
"designation": "Some Guy",
"location": "Global"
})
WriteResult({ “nInserted” : 1 })
var users =
[
{
"id": 1002,
"user_name": "jack",
"name": [
{"first_name": "Jack"},
{"middle_name": ""},
{"last_name": "Daniel"}
],
"email": "ao@gmail",
"designation": "Worker",
"location": "US"
},
{
"id": 1003,
"user_name": "Mark",
"name": [
{"first_name": "Mark"},
{"middle_name": "S"},
{"last_name": "Henry"}
],
"email": "ao@aodba",
"designation": "DBA",
"location": "US"
}
];
db.users.insert(users);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})