jeudi 2 août 2018

MongoDB how to get a single random document from a collection subset with Java driver?

I have a not-that-big collection of quite-big documents with a boolean field foo. What I want my java app to do is to randomly get a single document where foo: true.

I found a lot of stuff about getting N random document from a whole collection using $sample, but nothing about doing this in combination with a find query.

I don't expect the collection to grow too big, but documents can be quite big so getting the find result and then randomly picking app-side a single one may be too slow and resource wasting, so I need this to be completely server-side.

Thanks to anyone willing to help me.




Aucun commentaire:

Enregistrer un commentaire