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

To get inner values of JSON data in MongoDB, use the find() method along with dot notation to access nested fields within documents and arrays.

Syntax

db.collection.find(
    {}, 
    { "outerField.innerField": 1, "_id": 0 }
);

Sample Data

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")
}

Example 1: Extract Specific Nested Fields

To get only the Subject values from the otherdetails array ?

db.demo235.find({}, {"otherdetails.Subject": 1, "_id": 0});
{ "otherdetails": [ { "Subject": "MongoDB" }, { "Subject": "MySQL" } ] }

Example 2: Extract Multiple Nested Fields

To get both Name and Age from the details array ?

db.demo235.find({}, {"details.Name": 1, "details.Age": 1, "_id": 0});
{ "details": [ { "Name": "Chris Brown", "Age": 21 }, { "Name": "David Miller", "Age": 24 } ] }

Key Points

  • Use dot notation ("field.subfield") to access nested values in arrays and objects
  • Set _id: 0 in projection to exclude the ObjectId from results
  • The projection returns the nested structure while filtering specific fields

Conclusion

MongoDB's dot notation with find() projection allows precise extraction of inner JSON values from nested documents and arrays. This approach maintains the original structure while returning only the required fields.

Updated on: 2026-03-15T02:01:07+05:30

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements