Find inside a hash MongoDB?


To find inside a hash MongoDB, you can use dot(.) notation. Let us first create a collection with documents

> db.hashDemo.insertOne({"ClientName":"Larry","ClientAge":23,"ClientDetails":{
"isEducated": true, "ClientProject" : "University Management"}});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ca1ef1266324ffac2a7dc5e")
}
> db.hashDemo.insertOne({"ClientName":"Chris","ClientAge":26,"ClientDetails":{
"isEducated":false, "ClientProject" : "Online Book Store"}});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ca1ef7766324ffac2a7dc5f")
}

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

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

This will produce the following output

{
   "_id" : ObjectId("5ca1ef1266324ffac2a7dc5e"),
   "ClientName" : "Larry",
   "ClientAge" : 23,
   "ClientDetails" : {
      "isEducated" : true,
      "ClientProject" : "University Management"
   }
}
{
   "_id" : ObjectId("5ca1ef7766324ffac2a7dc5f"),
   "ClientName" : "Chris",
   "ClientAge" : 26,
   "ClientDetails" : {
      "isEducated" : false,
      "ClientProject" : "Online Book Store"
   }
}

Following is the query to find inside a hash MongoDB

> db.hashDemo.find({"ClientDetails.isEducated" : false, "ClientDetails.ClientProject" : "Online
Book Store"}).pretty();

This will produce the following output

{
   "_id" : ObjectId("5ca1ef7766324ffac2a7dc5f"),
   "ClientName" : "Chris",
   "ClientAge" : 26,
   "ClientDetails" : {
      "isEducated" : false,
      "ClientProject" : "Online Book Store"
   }
}

Updated on: 30-Jul-2019

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements