Insert document in collection
Two types of insert
- Insert single records:
- Insert multiple records
Insert single records:
syntax:
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
Example 1:
db.users.insertOne({"name":"ram","email":"ram@gmail.com"})
Example 2:
db.users.insertOne(
[{"employee":{"name":"ram","email":"ram@gmail.com"}}]
)
Insert multiple records:
syntax:
db.Collection_name.insertMany(
[<document 1>, <document 2>, …],
{
writeConcern: <document>,
ordered: <boolean>
})
Example:
db.users.insertMany([{"name":"ram","email":"ram@gmail.com"},{"name":"raju","email":"raju@gmail.com"}])