简体   繁体   English

如何使用Java驱动程序访问MongoDB中嵌套在数组中的对象

[英]How to access object nested inside an array in MongoDB using Java driver

{ "_id" : 0 , 
  "prices" : [ 
      { "type" : "house" , "price" :     10345} , 
      { "type" : "bed" , "price" : 456.94} , 
      { "type" : "carpet" , "price" : 900.45} , 
      { "type" : "carpet" , "price" : 704.48}
   ]
}

In avobe document how'll I delete the carpet which have lowest price using java driver? 在avobe文件中,如何使用Java驱动程序删除价格最低的地毯? ie, I've to delete { "type" : "carpet" , "price" : 704.48} 即,我必须删除{ "type" : "carpet" , "price" : 704.48}

Try this: 尝试这个:

   DBCursor cursor = collection.find(new BasicDBObject("prices.type", "carpet"),
                new BasicDBObject("prices", 1));

            try {
                while (cursor.hasNext()){
                    DBObject doc = cursor.next();

                    ArrayList<BasicDBObject> prices= (ArrayList<BasicDBObject>) doc.get("prices");

                    double minPrice = -1;

                    for(BasicDBObject dbObject: prices){
                        if (!dbObject.get("type").equals("carpet"))
                            continue;

                        double price= (Double) dbObject.get("price");

                        if (minPrice == -1) {
                            minPrice = price;
                            continue;
                        }

                        if (price< minPrice )
                            minPrice = price;
                    }

                    for (BasicDBObject dbObject: prices){
                        if (dbObject.get("type").equals("carpet") && (((Double) dbObject.get("price")) == minPrice)){
                            collection.update(new BasicDBObject("_id", doc.get("_id")),
                                    new BasicDBObject("$pullAll", new BasicDBObject("prices", Arrays.asList(dbObject))));
                            System.out.println(dbObject);
                        }
                    }
                }
            } finally {
                cursor.close();
            }

My solution can be not very well )) but I wanted show you an alternate variant 我的解决方案可能不太好)),但我想向您展示一个替代的变体

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

相关问题 如何使用 MongoDB Java 驱动程序更新数组中的 object? - How to update an object in a array using MongoDB Java Driver? 使用objectID对嵌套对象的MongoDB java驱动程序查询不起作用 - MongoDB java Driver query on nested object using objectID is not working 使用Java驱动程序3.2在MongoDB中更新嵌套的嵌入式数组文档 - Updating nested embedded array document in MongoDB using java driver 3.2 如何使用 MongoDB Java 在 Object 数组中获取字符串? - How to get a String inside an Object Array using MongoDB Java? 使用 Java 驱动程序过滤 mongodb 文档中的嵌入数组 object - Filter embedded Array object in mongodb document using Java driver 如何使用带有mongoDb的java3.2驱动程序删除类似数组对象的对象 - how to delete this like array object using java3.2 driver with mongoDb MongoDB:如何使用Java驱动程序获取包含数组的所有元素? - MongoDB : how to get all elements that contain an array using Java Driver? 如何使用java驱动程序将文档与mongodb中的现有数组元素进行匹配 - How to match a document with existing array elements in mongodb using java driver 使用Java驱动程序更新MongoDB中的数组 - Updating an array in MongoDB using Java driver 具有Java驱动程序的MongoDB-使用$ addToSet和updateOne将新集添加到嵌套数组 - MongoDB with Java driver - Adding a new set to a nested array using $addToSet and updateOne
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM