MongoDB: Insert Document

How can you insert Documents in MongoDB?

  1. In MongoDB, Documents are nothing but the record that is stored in the database.
  2. If Collections in MongoDB are compared to Tables then, you can describe the Documents as what you call rows in RDBMS(Database management system that store data in form of Tables).
  3. Therefore, it can be concluded that you can have several documents inside a collection. 
  4. The data in documents are stored in Schemaless form (ie. every document can follow its schema )or you can say in JSON Form( enclosed with {} and contains Key: Value pairs).
    for example:
    key_Document1: Value_Document1, key1_Document1: Value1_Document1


 1) To insert a single document, the insertOne() command is used:


2) To insert multiple documents, insertMany() command is used:



1)Let's try to create a collection named Products in MyDB Database and also add a single document that contains the name and cost of the product:

 db.products.insertOne({Name:'t-shirts', cost:2000})


 insert document1


It can be noticed that the above output returns the acknowledgment as True and also has returned a unique id for the document. 

2) Now, if You want to insert two documents into the Product collection, you can do it using insertMany(). Follow the code below for the same:

 db.products.insertMany([{Name:'trouser', cost:1000},{Name:'Skirts',cost:2450,fabric:{'Cotton':2,'Lycra':5}}])

NOTE: In the above code you can see 2 documents:

  • {Name:'trouser', cost:1000} 
  • {Name:'Skirts',cost:2450,fabric:{'Cotton':2,'Lycra':5}} which represents document embedded inside another document.