MongoDB从BasicDBObject(Java)中提取值 [英] MongoDB extracting values from BasicDBObject (Java)

查看:111
本文介绍了MongoDB从BasicDBObject(Java)中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从MongoDB中查询的文档中检索值。



例如,doc结构如下:

  {
_id:{
$ oid:50f93b74f9eccc540b302462
},
response:{
结果:{
代码:1000,
msg:命令已成功完成
},
resData:{
domain:infData:{
domain:name:ritesh.com,
domain:crDate:2007-06-15T12:02:36.0000Z,
domain:exDate:2013-06-15T12:02:36.0000Z
}
}
}
}

查询代码为:

  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();
对象值= obj.get(response.resData.domain:infData.domain:name);
}

它查询正常并提取文档,但我似乎无法想象如何从DBObject(或BasicDBObject,因为c.next()返回类型BasicDBObject)中提取response.resData.domain:infData.domain:name或其他类似嵌套值的值。



我可以一次获取一个对象,如:

 ((DBObject)obj.get( 回复))。get(resData).... 

但这看起来非常麻烦。



我想,因为你可以把()一个嵌套的字段值放在BasicDBObject中,如:

  basicDBObject.put(response.resData.domain:infData.domain:name,ritesh.com); 

我可以类似地使用get()来使用相同类型的密钥从BasicDBObject结果中获取。就像我在上面的代码中尝试做的那样:

 对象值= obj.get(response.resData.domain:infData 。域:名称); 

但是返回空值。



<这可能是直截了当的,但我似乎无法弄明白。在网上检查的每个地方,示例只从结果中获取未嵌套的值。喜欢

  doc.get(name); 

而不是类似:

  doc.get(name.lastname.clanname); 

任何帮助将不胜感激。谢谢!

解决方案

没有办法像使用Java驱动程序一样链接属性名称(肯定会得到,根据这个 put 也不应该工作)。



你需要像你建议的那样一次获得一个对象。

 ((DBObject)obj.get(response))。get(resData)

请参见此处一个潜在的未来功能,可以让你的语法可能工作(虽然,可能有一个新的方法名称)。


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!

解决方案

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

这篇关于MongoDB从BasicDBObject(Java)中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆