使用Java更新数组MongoDB中的元素
示例JSON文档使用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
{
"_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)
代码(完整)
如果知道要更新的元素的位置,则无法更新 所以基本上你能做的是:
基于评论的更新:
你能告诉我们你到目前为止尝试更新的内容吗?你读过更新数组字段的文档吗?可能重复@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
}
]
}
}
);