Mongodb Insert Row in Collection - MongoDB Tutorial



Mongodb Insert Row in Collection

  • In mongoDB all the row of datas are stored in the document oriented types they are called as “collection”.
  • All data stored into the collection are in BSON (Binary Script Object Notation) format.

Syntax :

db.collection.insert()  

Here in this syntax we can Insert the 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 Query1 :

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


Output

MongoDb
  • In this statement we display the table (or) collections.
  • Here in this statement we insert a data in the collection “wikitechy4” using “db.wikitechy4.insert” rich query and 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 data in the collection “wikitechy4” using insert rich query, where the data’s are inserted successfully as shown above.


Related Searches to Mongodb Insert Row in Collection - MongoDB Tutorial