1

Assume we have following collection.

{
    "Region":"Karnataka",
    "ShortCode":"KA",
    "SubRegion":[
        {
            "District":"Banglore",
            "Commodity":[
                {
                    "Name":"items",
                    "isActive":true,
                    "CommoditySubType":[
                        {
                            "Title":"Moistouriser",
                            "isActive":true,
                            "hasGrades":true,
                            "Grade":[
                                {
                                    "Title":"Premium",
                                    "Rate":"150",
                                    "isActive":true,
                                    "hasRates":true,
                                    "hasSizes":true,
                                    "StartDate":"2021-03-31",
                                    "EndDate":"2021-04-06",
                                    "StartTime":"9:00am",
                                    "EndTime":"6:00pm",
                                    "Sizes":[
                                        {
                                            "Title":"Small",
                                            "isActive":true
                                        }
                                    ]
                                    "LastSevenDaysDates":[{
                                    "Date":2021-03-31,
                                    "Price":"150"
                                    }]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
         "District":"Coorg",
            "Commodity":[]
        }
    ] }

I want to update an object under LastsevndayDates. I tried this.

    mongo.db.supplierDailyPrice.update(
{ 
    "Region":region,
    "SubRegion.District":district,
    "SubRegion.Commodity.Name":commodity,
    "SubRegion.Commodity.CommoditySubType.Title":commoditysubtype,
    "$and": [
        { "SubRegion.Commodity.CommoditySubType.Grade": { "$exists": True}},
        { "SubRegion.Commodity.CommoditySubType.Grade.Title": "Premium" },
        ]      
},
{
    "$set": {
        "SubRegion.$[].Commodity.$[].CommoditySubType.$[].Grade.$[].LastSevenDaysDates": Date
       
    }
})

But it's failing with the error pymongo.errors.WriteError: The path 'SubRegion.0.Commodity.0.CommoditySubType.1.Grade' must exist in the document in order to apply array updates.

0

2 Answers 2

1

You can use arrayFilters to solve your problem

db.supplierDailyPrice.update({
    "Region":region
},
{
    $set:{
        "Subregion.$[sub].Commodity.$[com].CommoditySubType.$[Cst].Grade.$[grd]. LastSevenDaysDates.$[sev].date": Date
    }
},
{
    array_filter : [
      {"sub.district": district},
      {"com.name": commodity},
      {"Cst.title": commoditysubtype},
      {"grd.title": "premium"},
      {"sev.price":"150"},
    ]
})

Not tested the code but something like this will solve your problem. For more reference you can check out the links below:

Update deeply nested array in mongodb

update deeply nested array mongodb

Pymongo error for ArrayFilters to update multiple subdocuments

Sign up to request clarification or add additional context in comments.

Comments

0

Ujjwal's answer is on the right track. I fixed a few typos.

More information here about $[] usage. https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/ https://developer.mongodb.com/how-to/update-array-elements-document-mql-positional-operators/

    db.supplierDailyPrice.update_one({
        "Region": region
    },
    {
        $set:{
            "SubRegion.$[sub].Commodity.$[com].CommoditySubType.$[cst].Grade.$[grd].LastSevenDaysDates.$[sev].Date": "2021-04-03"
        }
    },
    {
        "array_filters" : [
          {"sub.District": district},
          {"com.Name": commodity},
          {"cst.Title": commoditysubtype},
          {"grd.Title": grade},
          {"sev.Price":"150"},
        ]
    },
    {
       upsert=true
    }
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.