How can we find random document matching given query and update it atomically in mongoDB?

172 Views Asked by At

FindOneAndUpdate picks documents sequentially. Is there a way to pick a random document matching our query and update it atomically?

1

There are 1 best solutions below

4
On

To get a random document, use $sample.

To update it, use the aggregation pipeline in FindOneAndUpdate.