Refer :Insert Documents — MongoDB Shell
Insert Documents
The MongoDB shell provides the following methods to insert documents into a collection:
To insert a single document, use
db.collection.insertOne()
.To insert multiple documents, use
db.collection.insertMany()
.
Insert a Single Document
db.collection.insertOne()
inserts a single document into a collection. If the document does not specify an _id
field, MongoDB adds the _id
field with an ObjectId value to the new document.
insertOne()
returns a document that includes the newly inserted document's _id
field value.db.movies.find( { title: "The Favourite" } )
Insert Multiple Documents
db.collection.insertMany()
can insert multiple documents into a collection. Pass an array of documents to the method. If the documents do not specify an _id
field, MongoDB adds the _id
field with an ObjectId value to each document.
insertMany()
returns a document that includes the newly inserted documents' _id
field values.
To read documents in the collection: