Announcement Announcement Module
Collapse
No announcement yet.
$in operator array mongodb Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • $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
    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"));

    Comment


    • #3
      Hi,

      Thanks for replying.

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

      Comment


      • #4
        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)));

        Comment


        • #5
          Thanks! This works.

          Comment


          • #6
            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 ?

            Comment

            Working...
            X