Thursday, September 6, 2012

MongoDB: Search Nested Value With Java API

Let's say we have an object with structure like this:
{
  "name" : John,
  "contactInformation" : {
        email : “john@gmail.com”,
        phone: "12341621"
   }
}

We can query for the e.g. email field which is a nested field as follows with the Java API:
BasicDBObject searchCriteria = new BasicDBObject();
searchCriteria.put("contactInformation.email", emailToSearchFor);
DBObject result = collection.findOne(searchCriteria);

No comments:

Post a Comment