1

Here is an existing document:

{
    "_id": ObjectId("58bfe67c71043e8947d487c7"),
    "_class": "com.plash.configurator.model.GmailMailData",
    "useremail": "[email protected]",
    "threadidslist": [
     {
       "threadid": "15aac9952c924e6c",
       "subject": "dd",
       "messagelist": [
         {
           "messageid": "15aad9c4ebba913c",
           "timestamp": "Wed Mar 08 11:09:45 UTC 2017",
           "from": "[email protected]",
           "body": "<div dir=\"ltr\">ddd</div>\r\n",
           "labelid": "RECEIVED"
         }
       ]
     }
    ]
}

The query I am using is:

Update update = new Update();
update.addToSet("messagelist", gm); //gm is the object of messaglist
Criteria criteria =  Criteria.where("useremail").is(useremailid)
                             .and("threadidslist.threadid").is(threadid);
mongoTemplate.updateFirst(Query.query(criteria), update, GmailMailData.class);

This query inserts the object at the top level, but I want the object to be added inside messagelist.

How do I add a new object inside messagelist if useremail and threadid matches?

3
  • Can you try update.addToSet("threadidslist.$.messagelist", gm) ? Commented Mar 9, 2017 at 10:59
  • @veeram Yup,tried it now,but no luck. Commented Mar 9, 2017 at 11:19
  • Thanks @Veeram ,My criteria was wrong. Commented Mar 9, 2017 at 11:48

1 Answer 1

1

This query works:

 Query query = new Query(Criteria.where("useremail").is(useremailid).and("threadidslist.threadid").is(threadid));
            update.push("threadidslist.$.messagelist", gm);
            mongoTemplate.updateFirst(query, update, GmailMailData.class);
Sign up to request clarification or add additional context in comments.

Comments

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.