title: MongoDB extracting values from BasicDBObject (Java)
body: <p>I am having trouble retrieving values from queried documents in MongoDB.</p>
  
  <p>For example, the doc structure is like:</p>
  
  <pre><code>    {
          "_id": {
              "$oid": "50f93b74f9eccc540b302462"
          },
         "response": {
              "result": {
                  "code": "1000",
                  "msg": "Command completed successfully"
              },
              "resData": {
                  "domain:infData": {
                      "domain:name": "ritesh.com",
                      "domain:crDate": "2007-06-15T12:02:36.0000Z",
                      "domain:exDate": "2013-06-15T12:02:36.0000Z"
                  }
              }
          }
      }
  </code></pre>
  
  <p>And the query code is:</p>
  
  <pre><code>    DBCollection collection = db.getCollection("domains");
  
      BasicDBObject p = new BasicDBObject("response.resData.domain:infData.domain:name", "ritesh.com");
      DBCursor c = collection.find(p);
  
      while(c.hasNext()) {
          DBObject obj = c.next();
          Object value = obj.get("response.resData.domain:infData.domain:name");
      }
  </code></pre>
  
  <p>It queries fine and fetches the doc, but I can't seem to figure out how to extract the value of "response.resData.domain:infData.domain:name" or other similarly nested values from the DBObject (or BasicDBObject since c.next() returns type BasicDBObject).</p>
  
  <p>I could fetch the objects one at a time like:</p>
  
  <pre><code>    ((DBObject)obj.get("response")).get("resData")....
  </code></pre>
  
  <p>but that seems very cumbersome.</p>
  
  <p>I thought since you can put() a nested field value in BasicDBObject like:</p>
  
  <pre><code>    basicDBObject.put("response.resData.domain:infData.domain:name", "ritesh.com");
  </code></pre>
  
  <p>that I could similarly use get() to fetch from the BasicDBObject result using the same kind of key. Like I attempted to do in the code above with:</p>
  
  <pre><code>    Object value = obj.get("response.resData.domain:infData.domain:name");
  </code></pre>
  
  <p>But that is returning a null value.</p>
  
  <p>It's probably something straightforward, but I can't seem to figure it out. And everywhere I've checked on the net the examples only fetch values that aren't nested, from the result. Like</p>
  
  <pre><code>    doc.get("name");
  </code></pre>
  
  <p>instead of something like:</p>
  
  <pre><code>    doc.get("name.lastname.clanname");
  </code></pre>
  
  <p>Any help would be appreciated. Thanks!</p>