I\'m trying to query a collection using operands AND\'d together. I\'ve got the shell version working:
db.widgets.find({color: \'black, shape: \'round\', wei
Another way to solve same problem is using aggregation:
// To print results
Block printBlock = new Block() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
// get db connection and collection
MongoDatabase db= mongoClient.getDatabase("dbname");
MongoCollection collection= database.getCollection("collectionname");
collection.aggregate(Arrays.asList(Aggregates.match(Filters.eq("key1", "value1")),
Aggregates.match(Filters.eq("key2", "value2")),
Aggregates.match(Filters.eq("key3", "value3")))).forEach(printBlock);
For more details please refer the v 3.4 mongo Aggregation documentation.
http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/aggregation/