Home > Enterprise >  MongoDB limit number of document to cycle?
MongoDB limit number of document to cycle?

Time:09-21

I'm not trying to limit the number of results. I'm trying to find a way to only cycle N documents of the collection, when using

 collection->find([]);

Is there an option or flag for that? Using PHP Cheers.

CodePudding user response:

Maybe $sample is what you are looking for.

It will randomly select N documents from the collection:

db.collection.aggregate([ 
  { $sample: { size: 10 } } 
])
  • Related