Question

I have a collections

users
{
    "_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
    "first" : "Shivam",
    "connections" : [
        {
            "invit_made" : [ ],
            "fl_no" : 615,
            "date" : ISODate("2014-05-16T00:00:00Z"),
            "fl" : "LB",
            "TYP" : "ZLP",
            "invit_reciv" : [ ],     


        },
        {
            "invit_made" : [ ],
            "fl_no" : 615,
            "date" : ISODate("2014-05-20T00:00:00Z"),
            "fl" : "LB",
            "invit_reciv" : [ ],
            "TYP" : "ZLP",

        }

    ]
}

I am performing update accoding to date in connections.but wrong nested documented is updated in my case.

    db.users.update(
        { 'connections.TYP' : 'ZLP'
         ,'connections.fl' : 'LB'
         ,'connections.date' : ISODate("2014-05-20T00:00:00Z")
         }, 
        { '$addToSet' : {  
                         'connections.$.invit_reciv'  : { 'last' : 'abc'}
                        } 
        })

Actual Result I am getting.

    {
        "_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
        "first" : "Shivam",
        "connections" : [
            {
                "invit_made" : [ ],
                "fl_no" : 615,
                "date" : ISODate("2014-05-16T00:00:00Z"),
                "fl" : "LB",
                "TYP" : "ZLP",
                "invit_reciv" : [ 
                                     {
                                          "last" : "abc"
                                 }
                                 ],     


            },
            {
                "invit_made" : [ ],
                "fl_no" : 615,
                "date" : ISODate("2014-05-20T00:00:00Z"),
                "fl" : "LB",
                "invit_reciv" : [ ],
                "TYP" : "ZLP",

            }

        ]
    }

Result I expect

{
    "_id" : ObjectId("53738eb7ac8ee07007c1d75a"),
    "first" : "Shivam",
    "connections" : [
        {
            "invit_made" : [ ],
            "fl_no" : 615,
            "date" : ISODate("2014-05-16T00:00:00Z"),
            "fl" : "LB",
            "TYP" : "ZLP",
            "invit_reciv" : [ ],     


        },
        {
            "invit_made" : [ ],
            "fl_no" : 615,
            "date" : ISODate("2014-05-20T00:00:00Z"),
            "fl" : "LB",
            "invit_reciv" : [ 
              {
                      "last" : "abc"
              }

                            ],
            "TYP" : "ZLP",

        }

    ]
}

Please help me understand what is happening in present query and what is wrong with it.

Était-ce utile?

La solution

You should use $elemMatch with positional operator, it allows you to match specific nested doc:

db.users.update(
         { connections:{
              $elemMatch:{
                          'TYP' : 'ZLP',
                          'fl' : 'LB',
                          'date' : ISODate("2014-05-20T00:00:00Z")
                         }
                       }
          },
          { '$addToSet' : {  
                          'connections.$.invit_reciv' : 
                                      { 'last' : 'abc'}
                          } 
          }
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top