How to query MongoDB a value with lte, in and $not to fetch specific values?

To query MongoDB using $lte, $in, and $not operators together, you can combine these operators to fetch documents that meet specific criteria. These operators help filter documents based on comparison, inclusion, and negation conditions.

Syntax

db.collection.find({
    "field": {
        "$lte": value,
        "$in": [value1, value2, ...],
        "$not": { "$gt": value }
    }
});

Sample Data

Let us first create a collection with documents ?

db.demo130.insertMany([
    {
        "PlayerDetails": [
            {"Score": 56}, 
            {"Score": 78}, 
            {"Score": 89}, 
            {"Score": 97}
        ]
    },
    {
        "PlayerDetails": [
            {"Score": 45}, 
            {"Score": 56}
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e3065bf68e7f832db1a7f6d"),
        ObjectId("5e3065c068e7f832db1a7f6e")
    ]
}

Display all documents from the collection ?

db.demo130.find();
{ "_id": ObjectId("5e3065bf68e7f832db1a7f6d"), "PlayerDetails": [ { "Score": 56 }, { "Score": 78 }, { "Score": 89 }, { "Score": 97 } ] }
{ "_id": ObjectId("5e3065c068e7f832db1a7f6e"), "PlayerDetails": [ { "Score": 45 }, { "Score": 56 } ] }

Example: Query with $eq and $not

Find documents where PlayerDetails contains a Score equal to 56 but not greater than 56 ?

db.demo130.find({
    "PlayerDetails.Score": {
        "$eq": 56,
        "$not": { "$gt": 56 }
    }
});
{ "_id": ObjectId("5e3065c068e7f832db1a7f6e"), "PlayerDetails": [ { "Score": 45 }, { "Score": 56 } ] }

Key Points

  • $lte matches values less than or equal to the specified value
  • $in matches any of the values specified in an array
  • $not performs logical NOT operation on the specified expression
  • These operators can be combined within the same query condition

Conclusion

The $lte, $in, and $not operators provide powerful filtering capabilities in MongoDB queries. Combine them strategically to create precise query conditions that match your specific data requirements.

Updated on: 2026-03-15T02:12:25+05:30

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements