π $limit
If we return too many documents but we're interested in only a few, we can limit the number of documents returned using $limit.
- Instruqt
- Atlas UI
- MongoDB Shell
Open the 100_aggregation_pipeline_match file in the Instruqt IDE or GitHub Codespaces. Run the Startup Code, that will import all the neccessary modules and connect to the local MongoDB cluster.
const cursor = await books.aggregate( { $limit: 1 } );
await cursor.forEach((b) => {
console.log(b);
});
[
{ $limit: 1 }
]
db.books.aggregate([
{ $limit: 1 }
])
This returns just one document.
π Return just 7 books.
Answer
- Instruqt
- Atlas UI
- MongoDB Shell
const cursor = await books.aggregate( { $limit: 7 } );
await cursor.forEach((b) => {
console.log(b);
});
[
{ $limit: 7 }
]
db.books.aggregate([
{ $limit: 7 }
])