我无法从 MongoDB 中的查询文档中检索值.
I am having trouble retrieving values from queried documents in MongoDB.
比如doc结构是这样的:
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"
}
}
}
}
查询代码为:
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");
}
它查询良好并获取文档,但我似乎无法弄清楚如何从 DBObject(或 BasicDBObject)中提取response.resData.domain:infData.domain:name"或其他类似嵌套值因为 c.next() 返回类型 BasicDBObject).
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")....
但这看起来很麻烦.
我想既然您可以在 BasicDBObject 中放置()一个嵌套字段值,例如:
I thought since you can put() a nested field value in BasicDBObject like:
basicDBObject.put("response.resData.domain:infData.domain:name", "ritesh.com");
我可以类似地使用 get() 使用相同类型的键从 BasicDBObject 结果中获取.就像我试图在上面的代码中做的那样:
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");
但这会返回一个空值.
这可能很简单,但我似乎无法弄清楚.而且我在网上检查过的所有示例都只从结果中获取未嵌套的值.喜欢
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");
而不是类似的东西:
doc.get("name.lastname.clanname");
任何帮助将不胜感激.谢谢!
Any help would be appreciated. Thanks!
没有办法像使用 Java 驱动程序那样链接属性名称(肯定是 get
s,并且根据this, put
也不应该工作).
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).
这篇关于MongoDB 从 BasicDBObject (Java) 中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!