MongoDB limit number of document to cycle?

Viewed 20

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.

1 Answers

Maybe $sample is what you are looking for.

It will randomly select N documents from the collection:

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