I am having trouble retrieving values from queried documents in MongoDB.
For example, the doc structure is like:
{
\"_id\": {
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.
((DBObject)obj.get("response")).get("resData")
See here for a potential future feature that would allow your syntax to possibly work (although, likely with a new method name).
I ran into the same problem and I wrote a small function to fetch chained properties.
private Object getFieldFromCursor(DBObject o, String fieldName) {
final String[] fieldParts = StringUtils.split(fieldName, '.');
int i = 1;
Object val = o.get(fieldParts[0]);
while(i < fieldParts.length && val instanceof DBObject) {
val = ((DBObject)val).get(fieldParts[i]);
i++;
}
return val;
}
I hope it helps.