Friday, 4 June 2021

Mongo shell: Insert one document

‘insertOne’ method is used to insert one document to the collection.

 

Syntax

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

> db.employee.insertOne({"id" : 1, "name": "Krishna", "age" : 32, "designation" : "Architect"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("60ba0a678e67ad0ff5b02998")
}

 

When you insert a document into MongoDB, it automatically assigns a unique id to it. The type of this id is ObjectId. Document id is used to identify the document uniquely within the collection.

 

You can get all the documents from the collection using find method.

> db.employee.find()
{ "_id" : ObjectId("60ba0a678e67ad0ff5b02998"), "id" : 1, "name" : "Krishna", "age" : 32, "designation" : "Architect" }

 

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment