In Mongoose, you can write a statement like the following, to return documents with restricted fields, and only 10 of them. This returns the same 10 documents every time, which is expected.
products = await Product.find({
category: category,
})
.select("title img price desc category postageCost")
.limit(10);
I would like to show random products on page load and the mongoose .sample()
method is perfect, so I wrote this:
products = await Product.find({
category: category,
})
.select("title img price desc category postageCost")
.sample(10);
I can’t seem to make it work as I would expect though. Instead I had to run Product.find().sample(10)
and then modify the object returned, to only return the "title img price desc category postageCost" fields.
Is it possible to combine select()
and sample()
with Mongoose or is my method the only way to return random fields and restrict the contents of the returned documents?
2
Answers
You should use sample method with mongoose aggregate function, and then use project method to retrieve specific data.
You need to chain the
sample()
function as a method of theModel.aggregate
function but after you call thematch()
method. TheModel.aggregate.match()
method is synonymous with aModel.find()
.Here is an example: