To randomly select items from a collection using Mongoose, you can use the aggregate
method and the $sample
operator.
Here is an example of how to do it:
index.tsx203 chars10 lines
In this example, we are selecting 10 random items from the modelName
collection using $sample
operator. You can change the size to any value as per your requirement.
Note: The $sample
operator is only available in MongoDB version 3.2 or later. So, make sure you are using a compatible version of MongoDB.
gistlibby LogSnag