简体   繁体   English

从mongodb对象 - Java驱动程序返回属性

[英]Return attributes from mongodb object- Java driver

I have mongo object like 我有mongo对象喜欢

{
        "name" : "mongo",
        "values" : [
            {
                    "date" : "02-23-2015",
                    "price" : "5"
            },
            {
                    "date" : "02-23-15",
                    "price" : "6"
            },
            {
                    "date" : "02-22-15",
                    "price" : "7"
            }
    ]
}

I am working with Java driver and I am able to extract this object with a query like: 我正在使用Java驱动程序,我能够使用如下查询提取此对象:

QueryBuilder builder = new QueryBuilder();
DBObject nameQuery = new BasicDBObject("name", "mongo");
builder.and(nameQuery);
DBObject fullQuery = builder.get();

However what I want is to extract all of the price values and I want to store these in an array- right now I am only able to return this entire object..iterating through subsets like this is not explained very well in documentation, one example would be helpful. 然而我想要的是提取所有的价格值,我想将它们存储在一个数组中 - 现在我只能返回整个对象..通过像这样的子集在文档中没有很好地解释,一个例子会有所帮助。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: ok now I am able to iterate through the objects in this array but it does not take advantage of mongo built ins and will naturally be slower: 编辑:好了,我现在可以遍历这个数组中的对象,但它没有利用mongo内置的ins,自然会慢一些:

while (curs.hasNext())
    {
        DBObject o = curs.next();
        BasicDBList values = (BasicDBList) o.get("values");
        BasicDBObject[] valuesArray = values.toArray(new BasicDBObject[0]);
        for(BasicDBObject dbObj : valuesArray) {
            name = dbObj.getString("name");
         }
    }

This allows me to extract whatever I want but I know there is a more efficient way. 这允许我提取我想要的任何东西,但我知道有一种更有效的方法。 Any help would be great, thanks 任何帮助都会很棒,谢谢

I think it should work or it could carry you near of the solution: 我认为它应该工作或它可以带你接近解决方案:

    DB db = this.getConnection();
    DBCollection coll = db.getCollection(collection);
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", "mongo");

    cursor = coll.find(searchQuery);

    DBObject resultElement = null;
    List <String> prices = new ArrayList<String>(); 

    while(cursor.hasNext()){
            resultElement = cursor.next();
            prices.add ((String) resultElement.get("values.price"));
    }

Hope it helps. 希望能帮助到你。

The DBCollection.find() method takes a second parameter called keys which allows you to define which fields should be included in the result. DBCollection.find()方法接受一个名为keys的第二个参数,它允许您定义应该在结果中包含哪些字段。 Use it like this: 像这样使用它:

collection.find(query, new DBObject("values.price":1);

This will return a document with a values array containing only prices like this: 这将返回一个文档,其值数组仅包含如下价格:

{
    "values" : [
        {
                "price" : "5"
        },
        {
                "price" : "6"
        },
        {
                "price" : "7"
        }
]
}

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

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