Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

You can use dot(.) notation to read a specific key-value pair from MongoDB collection. Let us first create a collection with documents −

> db.readSpecificKeyValueDemo.insertOne(
...    {
...       "_id": 100,
...       "StudentDetails":
...       {
...          "StudentFirstName" : "David",
...          "StudentLastName" :"Miller",
...          "StudentAge":23,
...          "StudentCountryName":"US"
...       }
...    }
... );
{ "acknowledged" : true, "insertedId" : 100 }

Following is the query to display all documents from the collection with the help of find() method −

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

This will produce the following output −

{
   "_id" : 100,
   "StudentDetails" : {
      "StudentFirstName" : "David",
      "StudentLastName" : "Miller",
      "StudentAge" : 23,
      "StudentCountryName" : "US"
   }
}

Following is the query to read a specific key-value pair from a MongoDB collection −

> db.readSpecificKeyValueDemo.find({},{"StudentDetails.StudentCountryName":1}).pretty();

This will produce the following output −

{ "_id" : 100, "StudentDetails" : { "StudentCountryName" : "US" } }

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.