1

I want to update Itinirary which is embedded subdocument inside the packages which itself is a embedded document.I tried using index and was successful like this :

 "$set":{'packages.$.itinerary.0.to': "kausaltar"} 

but I don't want to use index like 0,1 in itinerary update.Please can you help me.

My JSON schema is like this :

  {
         "_id" : ObjectId("5e1ca76b9f96d17c449de177"),
        "org_id" : "22222222222",
        "packages" : [ 
            {
                "_id" : ObjectId("5e1ca76b9f96d17c449de17a"),
                "region" : "ppopopop",     


                "itinerary" : [ 
                    {
                        "_id" : ObjectId("5e1ca76b9f96d17c449de17d"),
                        "id" : 1,
                        "from" : "ppopopop",
                        "to" : "ashinnkn",
                        "mode" : "4444444444",
                        "day" : 2,
                        "duration_hrs" : 3
                    }, 
                    {
                        "_id" : ObjectId("5e1ca76b9f96d17c449de17c"),
                        "id" : 2,
                        "from" : "44444444444",
                        "to" : "Faketon2",
                        "mode" : "4444444444",
                        "day" : 2,
                        "duration_hrs" : 3
                    }, 
                    {
                        "_id" : ObjectId("5e1ca76b9f96d17c449de17b"),
                        "id" : 3,
                        "from" : "55555555555",
                        "to" : "ashin",
                        "mode" : "55555555",
                        "day" : 2,
                        "duration_hrs" : 3
                    }
                ],
                "image_url" : "sadfasfsa",
            }, 
            {
                "_id" : ObjectId("5e1ca76b9f96d17c449de178"),
                "region" : "bktll",
                "itinerary" : [ 
                    {
                        "_id" : ObjectId("5e1ca76b9f96d17c449de179"),
                        "id" : 1,
                        "from" : "mmkkkkm",
                        "to" : "ashin",
                        "mode" : "SkkkkA",
                        "day" : 2,
                        "duration_hrs" : 3
                    }
                ],
                "image_url" : "sadfasfsa",
           }
        ]
    }
    }

I want to update itinerary field inside packages which itself is an embedded document. And my code is like this :

 AgentPackage.update({
            "_id" :  mongoose.Types.ObjectId("5e1ca76b9f96d17c449de177"),
            "packages.region" : "ppopopop", 
            'packages.itinerary.id':  2,
      }, {$set: {'packages.itinerary.$$.from': "test" }}

I am not being able to update.I don't want to use indexing like 0,1 INSIDE query.

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46

2 Answers2

0

Try this :

If you've only one object inside packages with region : "ppopopop", then try this (this should suffice in most cases as you might not have duplicate regions with same name) :

AgentPackage.update({
    _id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop"
}, { $set: { "packages.$.itinerary.$[item].to": 'kausaltar' } }, {
    arrayFilters: [{ 'item.id': 2 }]
})

If you've got multiple objects inside packages with region : "ppopopop", then try this :

AgentPackage.update({
    _id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop" // Here this "packages.region": "ppopopop" is optional as _id will only bring one document else might be helpful to bring only docs with region: ppopopop right after filter. 
}, { $set: { "packages.$[eachPackage].itinerary.$[eachItinerary].to": 'kausaltar' } }, {
    arrayFilters: [{ 'eachPackage.region': "ppopopop" }, { 'eachItinerary.id': 2 }]
})

Ref : update-positional-filtered

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
  • @AshinMahat : Then can you please vote & accept answer which makes user know answers provided do work :-) – whoami - fakeFaceTrueSoul Jan 14 '20 at 19:24
  • It' working on windows but showing cannot use the part (itinerary of packages.0.itinerary.$[item].to) to traverse the element ({itinerary...... while working on ubuntu – Ashin Mahat Jan 22 '20 at 06:00
  • @AshinMahat : Why do you've to use `packages.0` ?? It can be dynamically iterated by using `packages.$[eachPackage]`... Also regarding ` 500 error in test cases` debug through what the issue is & post it as new question, Please do accept this if it's working for you, that way this question will be closed with an accepted answer.. – whoami - fakeFaceTrueSoul Jan 22 '20 at 16:15
  • 1
    Sorry,that was my collegue mongodb version was less then 3.So,it was not working in his laptop.After updating mongodb it's working good.Thank you bro – Ashin Mahat Jan 27 '20 at 15:36
0

You can do it with $arrayfilter

db.getCollection("unit").update({_id: ObjectId("5e1ca76b9f96d17c449de177")}, { $set: { "packages.$[t].itinerary.$[d].from": "test" } },
   { arrayFilters: [ { "t.region": "ppopopop" }, {"d.id":15}]})
  • It' working on windows but showing cannot use the part (itinerary of packages.0.itinerary.$[item].to) to traverse the element ({itinerary...... while working on ubuntu – Mean while its showing 500 error in test cases – – Ashin Mahat Jan 22 '20 at 06:06