Reputation: 2611
I need to append my field in mongodb
I am using below mongotemplate query for testing
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);
}
for testing purpose I am calling above code multiple times but its getting appended in my collection as a extra array .
It is updating as Array of Array
{
_id:5d5d55cde95b5c58740ec2d4
myId:123
mylist:[["Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You"]]
}
my expectation is
{
myId:123
_id:5d5d55cde95b5c58740ec2d4
mylist:["Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You","Hello", "World!", "How", "Are", "You"]
}
Upvotes: 1
Views: 2004
Reputation: 832
since pushAll is depreciated you have to use push and each together
update.push("mylist").each(dataList)
Upvotes: 3
Reputation: 3010
We need to use pushAll()
instead of push()
.
List<String> dataList = Arrays.asList("Hello", "World!", "How", "Are", "You");
Update update = new Update();
update.pushAll("mylist", dataList);
Upvotes: 0