1

Can anyone let me know how to find the random N documents from the collection having less than N documents. I tried using $sample which is resulting the maximum size of collection.

Here is the code which I have tried.

 question.aggregate([{$sample: {size: 12}}]);

question collection is having 10 records but I'm trying for the outcome of 12. When I tried as above, the result is having only 10 documents.

  • I suggest trying again. Statements without any evidence of a code attempt just ecourage copying from an existing answer. Show your code and we'll probably just point out your error in usage, but there's no code here at all. The operator works and you're not the first person to use it so I suggest you actually show what does not work. – Neil Lunn May 09 '18 at 12:03
  • Interesting case. You may have to call that in a loop until you have enough documents. – JohnnyHK May 09 '18 at 14:37
  • Thank you @JohnnyHK But we'll be having performance issue. So is there any more alternative? – Sindhu Garimella May 14 '18 at 06:58

0 Answers0