I am trying to query a collection using AND'd operands together. I have a shell version:
db.widgets.find({color: 'black, shape: 'round', weight: 100})
I cannot find the Java equivalent (using the native driver ). I tried different things, but here is my last attempt:
List<BasicDBObject> criteria = new ArrayList<BasicDBObject>();
criteria.add(new BasicDBObject("color", "black"));
criteria.add(new BasicDBObject("shape", "round"));
criteria.add(new BasicDBObject("weight", 100));
DBCursor cur = widgets.find(new BasicDBObject("$and", criteria));
List<Widget> widgetList = new ArrayList<Widget>();
DBCursor cur = widgets.find(andQuery);
while (cur.hasNext()) {
widgetList.add(new Widget(cur.next()));
}
if (widgetList.isEmpty())
System.out.println("No results found");
Any ideas what is wrong?
source
share