MongoDB extracting values from BasicDBObject (Java)

故事扮演 提交于 2019-12-28 04:04: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!


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




回答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.



来源:https://stackoverflow.com/questions/14410531/mongodb-extracting-values-from-basicdbobject-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!