

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Match MongoDB documents with field value greater than a specific number and fetch them?
To match, use $match in MongoDB. For values greater than a specific number, use $gt. Let us create a collection with documents −
> db.demo730.insertOne({"Name" : "Chris", "Marks" : 33 }); { "acknowledged" : true, "insertedId" : ObjectId("5eac54cd56e85a39df5f6339") } > db.demo730.insertOne({ "Name" : "David", "Marks" : 89}); { "acknowledged" : true, "insertedId" : ObjectId("5eac54cd56e85a39df5f633a") } > db.demo730.insertOne({ "Name" : "Chris", "Marks" : 45 }); { "acknowledged" : true, "insertedId" : ObjectId("5eac54ce56e85a39df5f633b") }
Display all documents from a collection with the help of find() method −
> db.demo730.find();
This will produce the following output −
{ "_id" : ObjectId("5eac54cd56e85a39df5f6339"), "Name" : "Chris", "Marks" : 33 } { "_id" : ObjectId("5eac54cd56e85a39df5f633a"), "Name" : "David", "Marks" : 89 } { "_id" : ObjectId("5eac54ce56e85a39df5f633b"), "Name" : "Chris", "Marks" : 45 }
Following is the query to match MongoDB documents with field value greater than a specific number and fetch them −
> db.demo730.aggregate([ ... { $sort: { _id: -1 } }, ... { $limit: 3 }, ... {$match: { $or: [ {"Name": "Chris", "Marks": { "$gt": 40 }}, {"Name": "David"} ]}} ... ])
This will produce the following output −
{ "_id" : ObjectId("5eac54ce56e85a39df5f633b"), "Name" : "Chris", "Marks" : 45 } { "_id" : ObjectId("5eac54cd56e85a39df5f633a"), "Name" : "David", "Marks" : 89 }
- Related Questions & Answers
- MongoDB query to match documents with array values greater than a specific value
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Fetch specific multiple documents in MongoDB
- MongoDB query (aggregation framework) to match a specific field value
- Fetch specific field values in MongoDB
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Find MongoDB documents that contains specific field?
- Find value above a specific value in MongoDB documents?
- Find documents that contains specific field in MongoDB?
- Count numbers with difference between number and its digit sum greater than specific value in C++
- MongoDB query to collectively match intersection of documents against a field
- Find MongoDB records with Price less than a specific value
- MongoDB query to match documents that contain an array field
- MongoDB to fetch documents with $or Operator
Advertisements