MongoDB文档使用Java中的findOneAndUpdate方法更新数组元素 [英] MongoDB document update array element using findOneAndUpdate method in Java

查看:1425
本文介绍了MongoDB文档使用Java中的findOneAndUpdate方法更新数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我们无法将数组插入字段"d",如下图所示,有人可以看一下这个问题吗:

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:

我们正在查询文档以更新ps数组中的任何字段,并在d字段中插入一个数组,如下所示.

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<Bson> list=new ArrayList<>();
         list.add(set);

         tim.findOneAndUpdate(where , set);

}

在上面的测试中,我们尝试在'th'字段中插入一个对象,该对象成功,但是对于'd'字段则不可能.请让我们知道我们在做什么错.请评论所需的任何信息. 预先感谢.

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<Bson> 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);

Mongo Shell中相同的更新操作:

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" } ]
   }
)





使用新值"newVal" 同时更新apn并将添加到d数组中的新字符串元素"gua"(如果该数组不包含,则将添加新数组)不存在):

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" } ]
   }
)

上面的Mongo Shell代码的Java代码:

The Java code for the above Mongo Shell code:

List<Bson> 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);

这篇关于MongoDB文档使用Java中的findOneAndUpdate方法更新数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆