Mongodb Insert Collection - MongoDB Tutorial



Mongodb Insert Collection

  • In mongoDB all the datas are stored in document oriented types they are called as “collection”.
  • All data stored into the collection are in BSON format.

Syntax

db.collection.insert() 

Here in this syntax we can Insert a document or documents into a collection.

Syntax for version 2.6 insert query

db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
) 
Parameters Type Description
document document (or) array
  • A document (or) array of documents to insert into the collection.
writeConcern document
  • Optional. A document expressing the write concern. Omit to use the default write concern.
ordered boolean
  • Optional.
  • If true, perform an ordered insert of the documents in the array, and if an error occurs with one of the documents, MongoDB will return without processing the remaining documents in the array.
  • If false, It performs an unordered insert, and if an error occurs with one of documents, continue processing the remaining documents in the array.

Sample Query 1

show collections         --display the created collection
db.wikitechy4.insert({"websitename":"wikitechy.com",
"details" : "learn mongodb basics and step by step",
"author" : "venkat"
})                       --insert data in a collection

MongoDb

Output

MongoDb
  1. In this statement we display the table (or) collections.
  2. Here in this statement we insert a data in the collection “wikitechy4” using “db.wikitechy4.insert” query where the data written successfully.

Sample Query 2

db.wikitechy4.insert({"websitename":"wikitechy.com",
"details" : "next row inserted",
"articlecount":"40",
"author" : "jln"
})

MongoDb

Output

MongoDb
  1. Here in this statement, we are adding another row in the “wikitechy4” collection using insert rich query where the data are inserted successfully as shown above.


Related Searches to Mongodb Insert Collection - MongoDB Tutorial