I am very new to MongoDB and its Java... SDK? Api? I have a very simple question, but I haven't been able to find a satisfactory answer.
Let's say I have a collection of instances that are like:
{
"_id": {
"$oid": "5156171e5d451c136236e738"
},
"_types": [
"Sample"
],
"last_z": {
"$date": "2012-12-30T09:12:12.250Z"
},
"last": {
"$date": "2012-12-30T04:12:12.250Z"
},
"section": "5156171e5d451c136236e70f",
"s_mac": "AA:AA:AA:AA:AA:AA",
"_cls": "Sample",
}
And I have a hard-coded Java list:
static List<String> MAC_LIST = Arrays.asList("90:27:E4:0E:3D:D2", "A8:26:D9:E6:1D:8B");
What I would like to know is how to query the MongoDB so it will give me all the objects whose s_mac field has a value that appears in the MAC_LIST List.
I'm guessing I should use the $in operator, but I don't know how to translate it to Java code.
Any hint or link to pages with explanations of the use of the $in operator through the Java SDK would be appreciated!
collection.find(new BasicDBObject("s_mac", new BasicDBObject("$in", MAC_LIST)));db.yourcollection.find({s_mac: {$in: ["90:27:E4...", "A8:26..."]}}). Also note that you can use a for-each loop with the result of a find:Iterable<DBObject> result = collection.find(); for (DBObject o : result) {...}> db.getCollection("myCollection").find({s_mac: {$in: ["90:27:E4:0E:3D:D2", "A8:26:D9:E6:1D:8B"]}}).count() 3