spring mongotemplate 查询追加数组正在创建数组的数组

spring mongotemplate query append an array is creating array of array

我需要在 mongodb

中附加我的字段

我正在使用下面的 mongotemplate 查询进行测试

List<String> dataList = Arrays.asList("Hello", "World!", "How", "Are", "You");

Update update = new Update();
     
update.push("mylist", dataList);
UpdateResult upResult = null;
query = new Query(Criteria.where("myId").is(appenModel.getMyId()));
try {
upResult = mongoTemplate.updateFirst(query, update, AppenModel.class);
logger.info("Updated Successfully");
} catch (Exception e) {
logger.error("Update is failed", e);
}

出于测试目的,我多次调用上面的代码,但它作为一个额外的数组附加在我的集合中。

它正在更新为 Array of Array

{
_id:5d5d55cde95b5c58740ec2d4
myId:123
   mylist:[["Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You"]]
}

我的期望是

{
myId:123
_id:5d5d55cde95b5c58740ec2d4
mylist:["Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You"]
}

我们需要使用 pushAll() 而不是 push()

List<String> dataList = Arrays.asList("Hello", "World!", "How", "Are", "You");
Update update = new Update();
update.pushAll("mylist", dataList);

由于 pushAll 已贬值,您必须同时使用 push 和 each

update.push("mylist").each(dataList)