java mongo updateone,Java中使用findOneAndUpdate方法更新MongoDB文档的数组元素

Can someone look at this issue as we are not able to insert the array in the field 'd' as shown in the image below:

6Li7K.jpg

We are querying the document to update any field in the ps array as well as insert a array in the d field as shown below.

public void updateTesting() {

Bson where = new Document().append("_id", 123456789012345L).append("session.ps.apn","abcdef");

Bson update1=new Document().append("th", "value3");

Bson update = new Document()

.append("session.ps.$.apn", "klo");

Bson set = new Document().append("$set", update).append("$addToSet", update1);

List list=new ArrayList<>();

list.add(set);

tim.findOneAndUpdate(where , set);

}

In the above for testing, we tried inserting an object in the 'th' field which is successful but the same was not possible for the 'd' field. Please let us know what we are doing wrong here. Please comment for any information required.

Thanks in advance.

解决方案

You have to use arrayFilters to update a specific array element (with a condition). The array filters in Java is defined with FindOneAndUpdateOptions object.

List arrFilters = new ArrayList<>();

arrFilters.add(new Document("elem.apn", "abcdef")); // this specifies the element search criteria

FindOneAndUpdateOptions updateOptions = new FindOneAndUpdateOptions().arrayFilters(arrFilters);

String [] dArray = { "app", "ban", "ora" }; // the "d" array to be added

Bson update = set("session.ps.$[elem].d", Arrays.asList(dArray));

String idStr = "5e37dc262f5ff4dfc935eb6b";

Bson queryFilter = eq("_id", new ObjectId(idStr));

Document result = coll.findOneAndUpdate(queryFilter, update, updateOptions);

System.out.println(result);

The same update operation in Mongo Shell:

var dArray = [ "app", "ban" ];

db.test.updateOne(

{ _id: ObjectId("5e37dc262f5ff4dfc935eb6b") },

{ $set: { "session.ps.$[elem].d" : dArray } },

{

arrayFilters: [ { "elem.apn": "abcdef" } ]

}

)

[EDIT ADD]

Updating the apn simultaneously with a new value "newVal" and adding a new string element "gua" to the d array (this will add a new array if the array doesn't exist):

db.test.updateOne(

{ _id: ObjectId("5e37dc262f5ff4dfc935eb6b") },

{

$set: { "session.ps.$[elem].apn": "newVal" }

$push: { "session.ps.$[elem].d" : "gua" }

},

{

arrayFilters: [ { "elem.apn": "abcdef" } ]

}

)

The Java code for the above Mongo Shell code:

List arrayFilters = new ArrayList<>();

arrayFilters.add(new Document("elem.apn", "abcdef"));

FindOneAndUpdateOptions updateOptions =

new FindOneAndUpdateOptions().arrayFilters(arrayFilters);

Bson pushUpdate = push("session.ps.$[elem].d", "gua");

Bson setUpdate = set("session.ps.$[elem].apn", "newValue");

Bson update = combine(pushUpdate, setUpdate);

String idStr = "5e37dc262f5ff4dfc935eb6b";

Bson queryFilter = eq("_id", new ObjectId(idStr));

Document result = coll.findOneAndUpdate(queryFilter, update, updateOptions);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值