MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?

To fetch a specific document from MongoDB where a field value is set using NumberInt(), use dot notation to target nested fields and match the exact NumberInt() value in your query.

Syntax

db.collection.find({"field.nestedField": NumberInt(value)});

Sample Data

Let us create a collection with documents containing NumberInt() values ?

db.demo357.insertMany([
    {
        "FirstName": "Chris",
        "Age": 21,
        "details": {
            "studentDetails": {
                "id": NumberInt(101)
            }
        }
    },
    {
        "FirstName": "David",
        "Age": 23,
        "details": {
            "studentDetails": {
                "id": NumberInt(110)
            }
        }
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e568fa6f8647eb59e5620c9"),
        ObjectId("5e568fbaf8647eb59e5620ca")
    ]
}

Display All Documents

db.demo357.find();
{ "_id": ObjectId("5e568fa6f8647eb59e5620c9"), "FirstName": "Chris", "Age": 21, "details": { "studentDetails": { "id": 101 } } }
{ "_id": ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName": "David", "Age": 23, "details": { "studentDetails": { "id": 110 } } }

Query Specific Document

Here is the query to fetch the document where the nested id field equals NumberInt(110) ?

db.demo357.find({"details.studentDetails.id": NumberInt(110)});
{ "_id": ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName": "David", "Age": 23, "details": { "studentDetails": { "id": 110 } } }

Key Points

  • Use dot notation to access nested fields like "details.studentDetails.id"
  • Match the exact NumberInt() value in your query condition
  • NumberInt() ensures 32-bit integer type matching

Conclusion

Query documents with NumberInt() fields using dot notation and exact value matching. MongoDB will return only documents where the specified nested field matches the NumberInt() value precisely.

Updated on: 2026-03-15T02:36:33+05:30

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements