V
V
Vearodev2020-09-18 01:48:25
MongoDB
Vearodev, 2020-09-18 01:48:25

Mongodb selecting random documents?

How can you get random documents from a collection using filters?

i.e. how can you get random documents with such a request?
find({ "tag": "test" })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-18
@Vearodev

https://docs.mongodb.com/manual/reference/operator...
something like this

db.posts.aggregate([ 
  { $match: { "tag": "test" } },
  { $sample: { size: 3 } } 
])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question