how to insert document in mongodb


How to insert document in Mongodb

db.Collection_Name.insert() method is use to insert document in Mongodb .

We can use db.Collection_Name.update() and db.Collection_Name.save() methods to insert document also.

Syntax

db.Collection_Name.insert()

Example

db.stud.insert(
{
 "roll":101,
 "name":"Vikash Kumar",
 "addr":"Hyderabad"   
}
)

Or We can insert Document Using save()

db.stud.save(
{
 "roll":101,
 "name":"Vikash Kumar",
 "addr":"Hyderabad"   
}
)

Or We can insert Document Using update()

db.stud.update(
{
 "roll":101,
 "name":"Vikash Kumar",
 "addr":"Hyderabad"   
}
)

Show Record

>db.stud.find()
{
    "_id" : ObjectId("56e3d55e24f2c2611ca9c28b"),
    "roll" : 101,
    "name" : "Vikash Kumar",
    "addr" : "Hyderabad"
}

Please observe we have added only three record but mongodb compiler added _id, here _id is primary key and it is Object type.






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229