Is there any way in MongoDB to get the inner value of json data?



To get inner value of JSON data, use find() along with dot(.) notation. Let us create a collection with documents −

> db.demo235.insertOne(
...   {
...      "id":101,
...      "details":[
...         {
...            "Name":"Chris Brown",
...            "Age":21
...         },
...         {
...            "Name":"David Miller",
...            "Age":24
...         }
...      ],
...      "otherdetails":[
...         {
...            "Score":56,
...            "Subject":"MongoDB"
...         },
...         {
...            "Score":78,
...            "Subject":"MySQL"
...         }
...      ]
...   }
...);
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e418d22f4cebbeaebec514b")
}

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

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

This will produce the following output −

{
   "_id" : ObjectId("5e418d22f4cebbeaebec514b"),
   "id" : 101,
   "details" : [
      {
         "Name" : "Chris Brown",
         "Age" : 21
      },
      {
         "Name" : "David Miller",
         "Age" : 24
      }
   ],
   "otherdetails" : [
      {
         "Score" : 56,
         "Subject" : "MongoDB"
      },
      {
         "Score" : 78,
         "Subject" : "MySQL"
      }
   ]
}

Following is the query to get the inner value of json data −

> db.demo235.find({},{"otherdetails.Subject":1,_id:0});

This will produce the following output −

{ "otherdetails" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] }

Advertisements