简体   繁体   English

如何从Java,MongoDB中的dbobject获取属性中的aa属性

[英]How to get a a property in a property from a dbobject in java, mongodb

So I have a few dbobjects in my mongo database. 所以我的mongo数据库中有几个dbobject。 Here's an example of one of the objects: 这是其中一个对象的示例:

{ "_id" : { "$oid" : "525b048580c3fb0d62d2b6fc"} , "city" : "London" , "currentWeather" : [ { "cloudcover" : "25"      , "humidity" : "82" , "observation_time" : "08:37 PM" , "precipMM" : "0.0" , "pressure" : "1008" , "temp_C" : "11" ,    "temp_F" : "52" , "visibility" : "10" , "weatherCode" : "113" , "weatherDesc" : [ { "value" : "Clear"}] , "weatherIconUrl" : [ { "value" : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"}] , "winddir16Point" : "W" , "winddirDegree" : "280" , "windspeedKmph" : "19" , "windspeedMiles" : "12"}]}

Now, I need to get all the dbobjects in my database whose value is lower than a given "temp_C", I have used something like this: 现在,我需要获取数据库中所有值低于给定“ temp_C”的数据库对象,我使用了以下方法:

BasicDBObject query = new BasicDBObject("temp_C", new BasicDBObject("&gt", graden));

But it's failing, and I think it is because the property is a subproperty of "currentWeather", yet I have no idea how to address this problem. 但是它失败了,我认为这是因为该属性是“ currentWeather”的子属性,但是我不知道如何解决该问题。 I am using java to do this. 我正在使用Java来做到这一点。

Looking at your document structure, you're trying to access a subdocument that lives inside an array in your document, so it's a bit more complicated than a standard query: 查看您的文档结构,您正在尝试访问位于文档数组中的子文档,因此它比标准查询要复杂一些:

{ "_id" : { "$oid" : "525b048580c3fb0d62d2b6fc"} , <-- Document
  "city" : "London" , 
  "currentWeather" : [                             <-- Array
                         { "cloudcover" : "25",    <-- Sub document
...etc...
                           "pressure" : "1008" , 
                           "temp_C" : "11",
                           "temp_F" : "52", 
...etc...
                         }
                     ]
}

In order to get to the nested object, you need to reference its position in the array (in this case, it's zero as it's the first element in the array) and then the field name in the sub document. 为了到达嵌套对象,您需要引用其在数组中的位置(在这种情况下,它是零,因为它是数组中的第一个元素),然后是子文档中的字段名。 So your query looks like this: 因此,您的查询如下所示:

BasicDBObject query = new BasicDBObject("currentWeather.0.temp_C", 
                                        new BasicDBObject("$gt", 11));

Note you had two problems in your original query: 请注意,原始查询中存在两个问题:

1) You need to reference currentWeather.0.temp_C 2) Your gt operator needs to start with a dollar sign not an ampersand. 1)您需要引用currentWeather.0.temp_C 2)您的gt运算符需要以美元符号开头,而不是与号。

Also, you said you wanted the query to return values lower than a given value, in which case you probably want $lt not $gt. 另外,你说你想查询返回低于给定值较低的值,在这种情况下,你可能需要$ LT不是$ GT。

You can't directly use the value of the object of an array in a query. 您不能在查询中直接使用数组对象的值。 You can use aggregate framework of Mongo. 您可以使用Mongo的聚合框架。 Java Docs For Aggregate are here Java Docs for Aggregate在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM