Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB/Java SDK: Query elements with a value in array

Tags:

java

list

mongodb

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!

like image 823
BorrajaX Avatar asked Apr 01 '13 20:04

BorrajaX


People also ask

How do you check an array contains a value in MongoDB?

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { <array field>: { <operator1>: <value1>, ... } }

How do I filter an array element in MongoDB?

Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

How do I find a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I query multiple values in MongoDB?

MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.


1 Answers

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.

like image 78
assylias Avatar answered Sep 28 '22 06:09

assylias