
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- 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
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Fetch specific field values in MongoDB
- Find MongoDB documents that contains specific field?
- Find value above a specific value in MongoDB documents?
- Find documents that contains specific field in MongoDB?
- MongoDB query to collectively match intersection of documents against a field
- MongoDB to fetch documents with $or Operator
- MongoDB query to match documents that contain an array field
- Fetch specific documents with array values in MongoD
- Count numbers with difference between number and its digit sum greater than specific value in C++

Advertisements