Inserted a document
The db.collection.insert() method adds new documents into a collection. Below command will insert a document into a collection named users.
var doc =
{
"Name": "Himen Patel",
"Email": "email2himen@xxx.com",
"Address1": "5619 Loyola",
"City": "Columbus",
"Zip Code": "43221",
"Country": "USA"
}
db.users.insert(doc);
The above command will create the collection if the collection does not exist in the document users and will returns a WriteResult object with the status of the operation.
A successful insert of the document returns the following object:
WriteResult({ "nInserted" : 1 })
The nInserted field will specifies the number of documents inserted. If the operation encounters an error, the WriteResult object will contain the error information.
Retrieving the inserted document
If the insert operation is successful, you can verify the insertion by querying the collection.
db.users.find()
or
db.users.find({Name: "Himen Patel"}) //By passing parameter with value
The document you inserted should return.
{ "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }