
- 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
How to query on top N rows in MongoDB?
To query on top N rows in MongoDB, you can use aggregate framework. Let us create a collection with documents
> db.topNRowsDemo.insertOne({"StudentName":"Larry","Score":78}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26eee6304881c5ce84b91") } > db.topNRowsDemo.insertOne({"StudentName":"Chris","Score":45}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26ef66304881c5ce84b92") } > db.topNRowsDemo.insertOne({"StudentName":"Mike","Score":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26efe6304881c5ce84b93") } > db.topNRowsDemo.insertOne({"StudentName":"Adam","Score":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26f066304881c5ce84b94") } > db.topNRowsDemo.insertOne({"StudentName":"John","Score":86}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26f0f6304881c5ce84b95") }
Following is the query to display all documents from a collection with the help of find() method
> db.topNRowsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca26eee6304881c5ce84b91"), "StudentName" : "Larry", "Score" : 78 } { "_id" : ObjectId("5ca26ef66304881c5ce84b92"), "StudentName" : "Chris", "Score" : 45 } { "_id" : ObjectId("5ca26efe6304881c5ce84b93"), "StudentName" : "Mike", "Score" : 65 } { "_id" : ObjectId("5ca26f066304881c5ce84b94"), "StudentName" : "Adam", "Score" : 55 } { "_id" : ObjectId("5ca26f0f6304881c5ce84b95"), "StudentName" : "John", "Score" : 86 }
Here is how you can query on top N rows in MongoDB
> db.topNRowsDemo.aggregate([ ... {$sort: {StudentName: 1}}, ... {$limit: 5}, ... {$match: {Score: {$gt: 65}}} ... ]).pretty();
This will produce the following output
{ "_id" : ObjectId("5ca26f0f6304881c5ce84b95"), "StudentName" : "John", "Score" : 86 } { "_id" : ObjectId("5ca26eee6304881c5ce84b91"), "StudentName" : "Larry", "Score" : 78 }
- Related Articles
- MySQL query to select top n rows efficiently?
- How to query on list field in MongoDB?
- Update multiple rows in a single MongoDB query?
- How to fire find query on sub-documents in MongoDB?
- MongoDB query to skip n first documents?
- MongoDB query to update array object in index N?
- How to query documents by a condition on the subdocument in MongoDB?
- Return query based on date in MongoDB?
- How to filter a query on specific date format with MongoDB?
- Query MongoDB subdocument to print on one line?
- How to query all items in MongoDB?
- MySQL query to return only the rows which are numeric?\n
- How to query MongoDB with “like”?
- How to implement MongoDB $or query?
- How to dynamically build MongoDB query?

Advertisements