I need to get a randomized sample of documents in a collection with a find criteria.
Bson sample = com.mongodb.client.model.Aggregates.sample(size);
BasicDBObject query = new BasicDBObject().append("myKey", value);
How can I combine this sample aggregation with the find query?
You can use aggregation with $match followed by $sample.
import static com.mongodb.client.model.Aggregates.*;
import static com.mongodb.client.model.Filters.*;
import static java.util.Arrays.asList;
Bson match = match(eq("myKey", value));
Bson sample = sample(size);
collection.aggregate(asList(match, sample));