Skip to main content

πŸ‘ $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.

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);
});

This returns just one document.

πŸ‘ Return just 7 books.

Answer
 const cursor = await books.aggregate(  { $limit: 7 } );

await cursor.forEach((b) => {
console.log(b);
});