MongoDB extracting values from BasicDBObject (Java

2020-01-29 07:18发布

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!

2条回答
劳资没心,怎么记你
2楼-- · 2020-01-29 07:51

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.

查看更多
劫难
3楼-- · 2020-01-29 07:53

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).

查看更多
登录 后发表回答