Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java更新数组MongoDB中的元素_Java_Arrays_Mongodb_Mongodb Query - Fatal编程技术网

使用Java更新数组MongoDB中的元素

使用Java更新数组MongoDB中的元素,java,arrays,mongodb,mongodb-query,Java,Arrays,Mongodb,Mongodb Query,示例JSON文档 { "_id" : "ab85cebc-8c7a-43bf-8efc-1151ccaa4f84", "address" : { "city" : "Bangalore", "postcode" : "560080", "countrycode":"in", "street" : "SADASHIVNAGAR,SANKEY TANK RD" }, "brand" : "Shell", "prices" : [ { "fue

示例JSON文档

{
"_id" : "ab85cebc-8c7a-43bf-8efc-1151ccaa4f84",
"address" : {
    "city" : "Bangalore",
    "postcode" : "560080",
    "countrycode":"in",
    "street" : "SADASHIVNAGAR,SANKEY TANK RD"
},
"brand" : "Shell",
"prices" : [ 
    {
        "fuelType" : "DIESEL",
        "price" : 52.7
    }, 
    {
        "fuelType" : "PETROL",
        "price" : 67.05
    }
]
}
  • 我在班加罗尔及周边地区有大约20份关于壳牌品牌的文件
  • 对于所有这些,我必须更新
    柴油
    汽油
    价格
假设当前的
柴油
汽油
价格分别为
57.9
71.4

如何使用
JAVA(使用eclipseide)

代码(完整)


如果知道要更新的元素的位置,则无法更新

所以基本上你能做的是:

  • 你需要查询才能找到这个职位
  • 使用“位置”操作符并更新阵列
  • 如果只想更新数组中嵌入的json元素内的特定字段,可以执行以下操作:


    基于评论的更新:


    你能告诉我们你到目前为止尝试更新的内容吗?你读过更新数组字段的文档吗?可能重复@titi23我写的这个查询是为了从MongDB
    mongocrsorsorcursor=db.getCollection(“gastation”).find(新的BasicDBObject(“address.city”,“Bangalore”).append(“brand”,“Shell”).iterator()请将其添加到问题中,然后尝试插入SO。您可以找到一些解决方案。如何将
    汽油机
    柴油机
    一起使用,以及如何在一个命令中使用20个文档?有可能吗?@shryasraob您可以对查询进行框架设置,使其获得所有20个或更多元素,并以相同的方式进行更新。实际上,基于您的应用程序中的上述代码更容易进一步调整way@ShreyasRaoB:实际上,如果您同时更新这两个位置,为什么需要查找/查找位置,您可以像覆盖一样盲目地设置这两个位置。这对你不管用吗?我是新来的,对如何编写查询也有点困惑。我需要使用查找/查找位置,因为FuelType和FuelType的价格不同。因此,如果一个文档同时包含柴油和汽油,我必须为每个文档更新具体的价格。现在它成功了!!!!谢谢你让我知道如何解决这个问题。。。
    public class UpdateGasstationFuelPrice {
    
    public static void main(String[] args) {
        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase db = client.getDatabase( "notes" );
    
        MongoCursor<Document> cursor = db.getCollection( "gasstation" ).find( new BasicDBObject( "address.countrycode","in" )
            .append("address.city","Bangalore")
            .append("brand","Shell")).iterator();
    
        if (cursor.hasNext()){
            Document doc = cursor.next();
        }
        client.close();
    }
    }
    
      db.getCollection("gasstation").update({"address.countrycode":"in","address.city":"Bangalore","brand":"Shell"},   
                //Query to get the position
                { 
                  "prices":  { $exists: true }  
                },
                // Use the positional $ operator to update specific element (which matches your query    
                { 
                  $set: 
                      { 
                        //set value specific to elements field/key
                        "prices" : [
                            {
                                "fuelType" : "DIESEL",
                                "price" : 502.7
                            }, 
                            {
                                "fuelType" : "PETROL",
                                "price" : 607.05
                            }
                          ] 
                      } 
                } 
            );
    
    db.gasstation.update(    
        //Query to get the position
        { 
          "prices.fuelType":  "DIESEL"  
        },
        // Use the positional $ operator to update specific element (which matches your query    
        { 
          $set: 
              { 
                "prices.$" : 
                  //Element/new value to update
                   {
                     "fuelType" : "DIESEL",
                     "price" : 999.7
                   } 
              } 
        } 
    );
    
    db.gasstation.update(    
        //Query to get the position
        { 
          "prices.fuelType":  "DIESEL"  
        },
        // Use the positional $ operator to update specific element (which matches your query    
        { 
          $set: 
              { 
                //set value specific to elements field/key
                //i.e. Update documents in an Array
                "prices.$.price" : 999.7 
              } 
        } 
    );
    
    db.gasstation.update(    
        //Query to match
        {
          "address.city":"Bangalore",
          "brand":"Shell",
          "countrycode":"in",
          "prices": { $exists: true }
        },
        // Use $set operator & overwrite entire array
        { 
          $set: 
              { 
                //Overwrite value
                "prices" : [
                    {
                        "fuelType" : "DIESEL",
                        "price" : 502.7
                    }, 
                    {
                        "fuelType" : "PETROL",
                        "price" : 607.05
                    }
                  ] 
              } 
        } 
    );