Results 1 to 6 of 6

Thread: $in operator array mongodb

  1. #1
    Join Date
    Aug 2011
    Posts
    6

    Default $in operator array mongodb

    If this is my document in mongo:

    { "_id" : ObjectId("4ea630b9e0eee526908d2614"), "address" : { "street" : "710 Montgomery St", "city" : "San Francisco", "state" : "CA" }, "latlong" : [ 37.795799, -122.403229 ], "name" : "Bocadillos", "phone" : "4159822622", "societies" : [ ObjectId("4e59693a9df7a393a15388e7") ] }


    How do I create a query that allows me to select a document based on the condition that a value(String) is in the societies array.

    So, if I get, "4e59693a9df7a393a15388e7', passed to the my Java method, how do I go about creating a Query utilizing the Criteria object.

    I tried the following, but it obviously doesn't work:

    Query filter = new Query(Criteria.where(socid).in("societies")); //socid = 4e59693a9df7a393a15388e7

  2. #2
    Join Date
    Dec 2008
    Location
    Aurora, CO, USA
    Posts
    24

    Default

    Since societies contains ObjectIds, you need to convert the socid to an objectId before passing it into the query

    Code:
    Query filter = new Query(Criteria.where(new ObjectId(socid)).in("societies"));

  3. #3
    Join Date
    Aug 2011
    Posts
    6

    Default

    Hi,

    Thanks for replying.

    I tried that, but Criteria.where(), only takes a String parameter, it does not take Objects.

  4. #4
    Join Date
    Dec 2008
    Location
    Aurora, CO, USA
    Posts
    24

    Default

    I was wrong; That's what I get for trying to answer quickly before leaving work. So now I double-checked the docs.

    the $in operator does the opposite of what you are trying to accomplish. It checks if the value of a field is in a list of values you provide (http://www.mongodb.org/display/DOCS/...dQueries-%24in). for instance, to see if state is either CA or FL,

    Code:
    Query filter = new Query(Criteria.where("state").in(new String[] { "CA", "FL" }));
    The normal is operator will check in arrays automatically (http://www.mongodb.org/display/DOCS/...ValueinanArray) so your code should be

    Code:
    Query filter = new Query(Criteria.where("societies").is(new ObjectId(socid)));

  5. #5
    Join Date
    Aug 2011
    Posts
    6

    Default

    Thanks! This works.

  6. #6
    Join Date
    Dec 2012
    Posts
    1

    Default

    Hi
    I have the same problem with my query with a little difference if i keep the example of @PraveenR :
    { "_id" : ObjectId("4ea630b9e0eee526908d2614"), "address" : { "street" : "710 Montgomery St", "city" : "San Francisco", "state" : "CA" }, "latlong" : [ 37.795799, -122.403229 ], "name" : "Bocadillos", "phone" : "4159822622", "societies" : [ "$ref" : "test", "$id" : ObjectId("4e59693a9df7a393a15388e7") ] }


    I tried the same thing as in the respond, and this one:
    mongoTemplate.find(new Query(Criteria.where("societies").elemMatch(
    Criteria.where("$id").is(id))), item.class);

    but it didn't work any ideas ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •