7

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!

9
  • Mmmm... how is Java going to know that the field I want to look for is actually "s_mac"? Commented Apr 1, 2013 at 21:00
  • 1
    Missed a bit: collection.find(new BasicDBObject("s_mac", new BasicDBObject("$in", MAC_LIST))); Commented Apr 1, 2013 at 21:02
  • Is not giving me any error, but apparently the resulting query doesn't have any "next" (and there's at least 2 instances int he collection with an s_mac contained in the array, though)... I'm gonna keep looking... It looks promising! :) Commented Apr 1, 2013 at 21:08
  • I just tried on a similar example and it worked as expected - not sure why you don't get anything back. You could try in the shell to see what you get: 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) {...} Commented Apr 1, 2013 at 21:15
  • 1
    Yeah, through the mongo terminal, I get 3 items whose s_mac is in the array > db.getCollection("myCollection").find({s_mac: {$in: ["90:27:E4:0E:3D:D2", "A8:26:D9:E6:1D:8B"]}}).count() 3 Commented Apr 1, 2013 at 21:26

1 Answer 1

4

Here is a contrived example that works for me (driver version 2.10.1) - you can adjust the IP address and run it as is to check if you get the same outcome:

public void gss() throws Exception{
    MongoClient mongo = new MongoClient("192.168.1.1");
    DB db = mongo.getDB("test");
    DBCollection collection = db.getCollection("stackoverflow");
    DBObject o1 = new BasicDBObject();
    o1.put("s_mac", "AA:AA:AA:AA:AA:AA");
    o1.put("_cls", "Sample1");
    DBObject o2 = new BasicDBObject();
    o2.put("s_mac", "90:27:E4:0E:3D:D2");
    o2.put("_cls", "Sample2");
    DBObject o3 = new BasicDBObject();
    o3.put("s_mac", "A8:26:D9:E6:1D:8B");
    o3.put("_cls", "Sample3");
    collection.insert(o1, o2, o3);
    System.out.println(collection.find().count());
    List<String> MAC_LIST = Arrays.asList("90:27:E4:0E:3D:D2", "A8:26:D9:E6:1D:8B");
    System.out.println(collection.find(new BasicDBObject("s_mac", new BasicDBObject("$in", MAC_LIST))).count());
}

It inserts the following documents:

{ "_id" : ObjectId("5159ff98567e143bff0668e9"),
  "s_mac" : "AA:AA:AA:AA:AA:AA",
  "_cls" : "Sample1"
}
{ "_id" : ObjectId("5159ff98567e143bff0668ea"),
  "s_mac" : "90:27:E4:0E:3D:D2",
  "_cls" : "Sample2"
}
{ "_id" : ObjectId("5159ff98567e143bff0668eb"),
  "s_mac" : "A8:26:D9:E6:1D:8B",
  "_cls" : "Sample3"
}

A call to collection.find().count() returns 3 and a call to collection.find(new BasicDBObject("s_mac", new BasicDBObject("$in", MAC_LIST))).count() returns 2 which I think is what you expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, actually, what you said in your comment was working. It was my freaking fault it wouldn't work! But this is a very good answer. I'm sure it'll help many people (is the first time one of my questions gets 3 upvotes in like... 20 minutes, which makes me believe that many people were wondering the same). Thank you again!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.