Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to query MongoDB a value with $lte, $in and $not to fetch 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 } ] }Advertisements