1

I want to insert a value in the array of the sub document when the value is not already there.I have a collection like this

 {
    "_id" : ObjectId("57ff25a74be508216afa9e03"),

    "RTODetails" : [
        {
            "pincode" : "34tgefgef",
            "state_name" : "Assam",
            "PUCDetails" : [
                {
                    "PUC_number" : "PUC755757",
                    "puc_year" : "2016"
                }
            ]
        }
    ]
    }

I want to add another array under PUCDetails like

{
    "_id" : ObjectId("57ff25a74be508216afa9e03"),

    "RTODetails" : [
        {
            "pincode" : "34tgefgef",
            "state_name" : "Assam",
            "PUCDetails" : [
                {
                    "PUC_number" : "PUC755757",
                    "puc_year" : "2016"
                },
                {
                    "PUC_number" : "PUC755757111",
                    "puc_year" : "2017"
                }
            ]
        }
    ]
}

I am using

db.collection('vehicleDetails').update(
    { "_id": new ObjectId('57ff25a74be508216afa9e03') },
    {
        $push: {
            "RTODetails.$.PUCDetails":{
                "PUC_number" : "PUC755757111",
                "puc_year" : "2017"
            }
        }
    }
)

but it's not working.

chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    refer this link: http://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array – chirag Oct 14 '16 at 10:34
  • Already Tried the answer of the given link as specified in my question but it is not working in my case. I have already checked my _id. – Vijay pandey Oct 14 '16 at 11:08

0 Answers0