MongoDB extracting values from BasicDBObject (Java)

21,212

Solution 1

There's no way to chain a property name like you're doing using the Java driver (gets 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).

Solution 2

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.

Share:
21,212
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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!

  • Esteve
    Esteve about 9 years
    You could use fieldName.split("\\.") to not depend on StringUtils