0

Hi i need help in MONGO DB Query below is my sample object:

"_id" : ObjectId("59310db171043e6154e89a49"),
"_class" : "com.Company",
"prospectid" : "1495715281281",
"prospectemailid" : "aaa@plash.in",
"useremail" : "xyx@xyx.com",
"campaignlist" : [ 
    {
        "campaignid" : "1497813578341",
        "subject" : "Camp",
        "messagelist" : [ 
            {
                "timestamp" : "2017-06-02 12:33:13",
                "from" : "abc@abc.com",
                "to" : "xyz@xyz.in",
                "body" : "<div><!--block-->Please reply to this mail.<br><br>I Look forward to<a href=\"https:div><div><!--block-->The most telling public commen" src=\5281281/1496453915668/1495398618921\" alt=\"\" style=\"width:1px;height:1px;\"/>",
                "labelid" : "SEND",
                "emailuniqueid" : "1496453915668",
                "messageid" : "",
                "messageid_tp" : "1495398618921",
                "cc" : ""
            }
        ]
    }, 

I have to add a new object inside messagelist based on some criteria i.e useremailid,prospectemailid and campaignid.

Below is my query:

 Query query = new Query(Criteria.where("useremail")
               .is(useremailid)
               .and("prospectemailid")
               .is(prospectemailid)
               .and("campaignlist.campaignid")
               .is(campaignid));

 update.push("campaignlist.$.messagelist", om); //om is the object of messageList

 mongoTemplate.updateFirst(query, update, OtherEmailData.class);

But sometimes the object get inserted inside message list sometimes does not> I am unble to figure out why it is happening. Please Help.

I am using SpringBoot.

utsav anand
  • 385
  • 1
  • 4
  • 16
  • Also referenced in the [answer with most votes](https://stackoverflow.com/questions/18573117/updating-nested-arrays-in-mongodb-via-mongo-shell) there is the documentation section for the [positional `$` operator on Nested Arrays](https://docs.mongodb.com/manual/reference/operator/update/positional/#nested-arrays). MongoDB cannot match beyond the "outer" array index. Various approaches are mentioned there, but really you should try to "flatten" your data structure to no more than 1 array deep. – Neil Lunn Jun 02 '17 at 09:22
  • @NeilLunn At this point of time i cannot flatten data structure, And the query is working 80% of the time. – utsav anand Jun 02 '17 at 09:27
  • Are you only **ever** going to push? And absolutely **never** attempt to match on any property of "campaignlist.messagelist"? Because if you are doing that, then hence the referenced problem. So please check that your 20% failure is not in fact actually writing to another index than the one you expect because of the known limitation. – Neil Lunn Jun 02 '17 at 09:31
  • @NeilLunn Yes everytime i am pushing new object to messagelist without matching any property of campaignlist.messagelist – utsav anand Jun 02 '17 at 10:39
  • Please narrow this down and show an exact case. You need to show. 1.) The document you expect to update 2.) The query conditions given 3.) The return value from the update showing that `nMatched` was indeed at least `1` yet `nModified` is `0`. Really check, because the most likely cause is that nothing is matching or that it is matching elsewhere to the array index you expect. If there is more information added to that effect that looks reasonable to solve, then your question is valid to reopen. But you do need to show something. – Neil Lunn Jun 02 '17 at 10:44

0 Answers0