We can INSERT documents in MongoDB’s collections by two methods

  1. insert()
  2. save()

INSERT ( ) METHOD

SYNTAX:

db.<COLLECTION_NAME>.insert( )

EXAMPLE:

db.student.insert({id : 1, name : "sachin" , age : 27 })
WriteResult({ "nInserted" : 1 })

Check Inserted Record

db.student.find().pretty()
{
 "_id" : ObjectId("5a2300fcab980773d4ff3175"),
 "id" : 1,
 "name" : "sachin",
 "age" : 27
}

EXPLAINATION

_id parameter, MongoDB assigns a unique ObjectId for each document.

_id is 12 bytes hexadecimal number unique for every document in a collection.

_id: ObjectId is divided in

4 bytes timestamp,
3 bytes machine id,
2 bytes process id,
3 bytes incrementer