1

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

2 Answers 2

3

since pushAll is depreciated you have to use push and each together

update.push("mylist").each(dataList)
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Push all is depreciated.. so can not use it .. but I don't know how to use push with each
@kcoder update.push("mylist").each(dataList)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.