I am having trouble retrieving values from queried documents in MongoDB.
For example, the doc structure is like:
{
"_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"
}
}
}
}
And the query code is:
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");
}
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).
I could fetch the objects one at a time like:
((DBObject)obj.get("response")).get("resData")....
but that seems very cumbersome.
I thought since you can put() a nested field value in BasicDBObject like:
basicDBObject.put("response.resData.domain:infData.domain:name", "ritesh.com");
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:
Object value = obj.get("response.resData.domain:infData.domain:name");
But that is returning a null value.
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
doc.get("name");
instead of something like:
doc.get("name.lastname.clanname");
Any help would be appreciated. Thanks!
I ran into the same problem and I wrote a small function to fetch chained properties.
I hope it helps.
There's no way to chain a property name like you're doing using the Java driver (
get
s for sure, and according to the this,put
isn't supposed to work either).You'll need to get the objects one at a time like you suggested.
See here for a potential future feature that would allow your syntax to possibly work (although, likely with a new method name).