Set variable value in MongoDB save() method


Use db.yourCollectionName.save(yourVariableName) to set variable value, wherein “yourVariableName” is your variable.

Let us see an example and create a variable −

> var Info={"Name":"David",
... "CountryName":"US",
... "ProjectDetails":[{"ClientName":"David","ProjectName":"Online Banking System"}]}

Following is the query to set the variable value in the save() to save the value in the collection −

> db.demo483.save(Info);
WriteResult({ "nInserted" : 1 })

Display all documents from a collection with the help of find() method −

> db.demo483.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e82e0d6b0f3fa88e22790a0"),
   "Name" : "David",
   "CountryName" : "US",
   "ProjectDetails" : [
      {
         "ClientName" : "David",
         "ProjectName" : "Online Banking System"
      }
   ]
}

Updated on: 11-May-2020

880 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements