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

MongoDBDatabaseBig Data Analytics

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 } ] }
raja
Published on 31-Mar-2020 15:22:54
Advertisements