Query MongoDB with LTE Operator and Exclude Specific Values



Let us first create a collection with documents −

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

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

> db.demo130.find();

This will produce the following output −

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

Following is the query to fetch −

> db.demo130.find({
...    "PlayerDetails.Score": {
...       "$eq": 56,
...       "$not": { "$gt": 56}
...    }
... })

This will produce the following output −

{ "_id" : ObjectId("5e3065c068e7f832db1a7f6e"), "PlayerDetails" : [ { "Score" : 45 }, { "Score" : 56 } ] }
Updated on: 2020-03-31T11:53:29+05:30

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements