
- 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
Get the maximum mark records from a collection with documents in MongoDB
To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −
> db.demo240.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9d9af932883c61ea3e") } > db.demo240.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441da19af932883c61ea3f") }
Display all documents from a collection with the help of find() method −
> db.demo240.find();
This will produce the following output −
{ "_id" : ObjectId("5e441d969af932883c61ea3c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e441d9a9af932883c61ea3d"), "StudentName" : "Bob" } { "_id" : ObjectId("5e441d9d9af932883c61ea3e"), "StudentName" : "David" } { "_id" : ObjectId("5e441da19af932883c61ea3f"), "StudentName" : "Mike" }
Following is the query to limit the number of records in MongoDB −
> db.demo240.aggregate( { $limit : 3 });
This will produce the following output −
{ "_id" : ObjectId("5e441d969af932883c61ea3c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e441d9a9af932883c61ea3d"), "StudentName" : "Bob" } { "_id" : ObjectId("5e441d9d9af932883c61ea3e"), "StudentName" : "David" }
- Related Articles
- Get the maximum mark records from a collection with documents in MongoDB query
- MongoDB query to get minimum and maximum value from documents including some duplicate records
- Get MongoDB documents with max attribute per group in a collection?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Find a value in lowercase from a MongoDB collection with documents
- Get the maximum element in MongoDB collection?
- Evaluate one of more values from a MongoDB collection with documents
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- Get the count of the number of documents in a MongoDB Collection?
- How to delete all the documents from a collection in MongoDB?
- Fetching all documents from MongoDB Collection in a beautified form
- Retrieving specific documents from collection by _id in MongoDB
- Display only a single field from all the documents in a MongoDB collection
- How to get latest set of data from a MongoDB collection based on the date records?

Advertisements