Update nested sub Document with a specific condition

I need a help for my updateMany query,

My Document Structure is :
{
   "_id":{
      "$oid":"61b09087c6379653642cc9d6"
   },
   
   "name":"Seven Rocks Mens Tshirt",
   "sku":"XXXL-T1-TMBL",
   "asin":"B07QMCLYYZ",
   "productIdealConsumption":[
      {
         "id":8470,
         "productIdealConsumptionId":663,
         "productId":1212,
         "qty":0.22,
         "product":{
            "id":1212,
            "sku":"6040CP-SJ-TB-FR-TM",
            "name":"Fabric Roll",
            "categoryId":5
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8471,
         "productIdealConsumptionId":663,
         "productId":1028,
         "qty":0.036,
         "product":{
            "id":1028,
            "sku":"6040CP-SJ-TB-FR-BL",
            "name":"Fabric Roll",
            "categoryId":5
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8472,
         "productIdealConsumptionId":663,
         "productId":1342,
         "qty":0.021,
         "product":{
            "id":1342,
            "sku":"CPL-1X1 RB-TB-FR-BL",
            "name":"Rib Fabric Roll",
            "categoryId":14
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
            
      },
      {
         "id":8473,
         "productIdealConsumptionId":663,
         "productId":2907,
         "qty":136.8,
         "product":{
            "id":2907,
            "sku":"GI-MTR-SEW.THD-2PLY-TM",
            "name":"2PLY",
            "categoryId":72
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0       
            
      },
      {
         "id":8474,
         "productIdealConsumptionId":663,
         "productId":2901,
         "qty":3.75,
         "product":{
            "id":2901,
            "sku":"GI-MTR-SEW.THD-2PLY-BL",
            "name":"2PLY",
            "categoryId":72
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8478,
         "productIdealConsumptionId":663,
         "productId":3058,
         "qty":1,
         "product":{
            "id":3058,
            "sku":"QCI-PCS-WASH CARE LBL-30MM*200MTR",
            "name":"WASH CARE LBL-30MM*200MTR",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
      },
      {
         "id":8479,
         "productIdealConsumptionId":663,
         "productId":3059,
         "qty":1,
         "product":{
            "id":3059,
            "sku":"QCI-PCS-WASH CARE RIBBON-40MM*300MTR",
            "name":"WASH CARE RIBBON-40MM*300MTR",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
         },
      {
         "id":8480,
         "productIdealConsumptionId":663,
         "productId":3046,
         "qty":1,
         "product":{
            "id":3046,
            "sku":"QCI-PCS-EB TAGS",
            "name":"EB TAGS",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
        },
      {
         "id":8481,
         "productIdealConsumptionId":663,
         "productId":3049,
         "qty":1,
         "product":{
            "id":3049,
            "sku":"QCI-PCS-PACKING GATTA- 10*13",
            "name":"PACKING GATTA- 10*13",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0
		 },
      {
         "id":8482,
         "productIdealConsumptionId":663,
         "productId":3053,
         "qty":1,
         "product":{
            "id":3053,
            "sku":"QCI-PCS-PLAIN BOPP BAG-11*16-21MIC.",
            "name":"PLAIN BOPP BAG-11*16-21MIC.",
            "categoryId":62
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
         },
      {
         "id":8483,
         "productIdealConsumptionId":663,
         "productId":3083,
         "qty":0.001,
         "product":{
            "id":3083,
            "sku":"QCI-LTR-BENZENE CHM.",
            "name":"BENZENE CHM.",
            "categoryId":66
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
          },
      {
         "id":8484,
         "productIdealConsumptionId":663,
         "productId":2895,
         "qty":0.75,
         "product":{
            "id":2895,
            "sku":"GCI-MTR-BONE DORI (MOTI)",
            "name":"BONE DORI (MOTI)",
            "categoryId":71
         },
         "avlInventory":500,
         "reservedInventory":2.25,
         "currAvlInventory":500,
         "skuAvlIntventoryQty":0,
         }
   ],
   "totalOprationTime":38,
   "totalOprations":38,
   "productId":57,
   "skuIdealConsumptionAvlIntventory":1,
   "skuOperationsTimeAvlIntventory":0,
   "skuAvlIntventory":0,
   "__v":0
} 

My query is like this,

finishedproducts.updateMany({
   "sku":{
      "$in":[
         "XS-T1-WMBL",
         "S-T1-WMBL",
         "M-T1-WMBL",
         "L-T1-WMBL",
         "XL-T1-WMBL",
         "XXL-T1-WMBL",
         "XXXL-T1-WMBL",
         "XS-T1-NMBL",
         "S-T1-NMBL",
         "L-T1-NMBL",
         "XL-T1-NMBL",
         "XXL-T1-NMBL",
         "XXXL-T1-NMBL",
         "XS-T1-KMBL",
         "S-T1-KMBL",
         "M-T1-KMBL",
         "L-T1-KMBL",
         "XL-T1-KMBL",
         "XXL-T1-KMBL",
         "XXXL-T1-KMBL",
         "XS-T1-BLAG",
         "S-T1-BLAG",
         "M-T1-BLAG",
         "L-T1-BLAG",
         "XL-T1-BLAG",
         "XXL-T1-BLAG",
         "XXXL-T1-BLAG",
         "XS-T1-TMBL",
         "S-T1-TMBL",
         "M-T1-TMBL",
         "L-T1-TMBL",
         "XL-T1-TMBL",
         "XXL-T1-TMBL",
         "XXXL-T1-TMBL"
      ]
   },
   "productIdealConsumption.$.productId":2895
},
[
   {
      "$set":{
         "productIdealConsumption.$.reservedInventory":"$productIdealConsumption.$.qty"
      }
   }
],
{
   
})

I want to update all subdocument “productIdealConsumption” which productId is “2895” with given document “skus”.
My query given me error , what wrong with my query.
Please look once my query, and suggest me a solution for this.
Thanks.

I gave a quick look at your query.

You do not use $ in the query part. You should replace

with
"productIdealConsumption.productId":2895

See https://docs.mongodb.com/manual/reference/operator/update/positional/ for examples. You might need to use https://docs.mongodb.com/manual/reference/operator/update/positional-all/ if more than one productIdealConsumption item refers to the same productId within the same top level document. This is not the case in the sample document you supplied but you should be aware.

Your problem is the same as Changing value in nested array from a scalar to an arry.

What is unresolved is how to get the positional value, productIdealConsumption.$.qty in your case. As mentioned in the other thread $map is worth investigating.

Another avenue would be to $unwind, update the unwinded documents, then $group back all documents and use $out or $merge.

See my answer at Mongo query assistance update array element with field from object - #5 by steevej

@Seekex_youtube, is your issue resolve? If it is, please mark my post as the solution so that others know that it works. This will help keep this post useful and efficient. It is simply courtesy.

Need Help…!!!
What If I want to check two conditions. Like I want to update a field only when two given conditions are true. If either of are false, I don’t want to update that field in that particular document. I also have a sub-doc like following
team: [ {field1, field2, field3,…}, {field1, field2, field3,…}, {field1, field2, field3,…}, …]

So, what I want to do is to check if field1 and field2 are satisfied or not. If they both are true/satisfied, make the update to field3, otherwise if either of it are false or not satisfied, don’t make any update.

Share real sample documents we can cut-n-paste into our system.

Share the expected result.

Share the exact condition you want.

Share what you have tried and explain us how it failed to deliver the expected result.

So, without going into other details of my project, I have a collection containing multiple documents. In each document, there is an array named “team” and inside this array, I have objects. Each individual object contain information for a particular player like name, points, _id and isCaptain (which tells that whether this particular player is a captain of this team or not in the form of ‘true’ and ‘false’ i.e isCaptain: true means that this player is captain of this team and isCaptain: false means that this player is not a captain of this team). From the frontend, I am sending two values; id and points. Now, what I am doing is that I’m fetching/reaching out to all the players(objects) whose _id matches with the input ‘_id’ (coming from the frontend). After this, I want to check if this player is the captain of this team or not. If he is, then I want to $set the points of this player as double points by doing points 2. And if that player is not a captain, then I just want to $set points to the input points without making it double.
I was able to do everything but I’m stuck on isCaptain. I want my code to be modelled in such a way that it checks the matching _id and also check that if that player is captain or not. If he is captain than double the points (points
2) otherwise go to the other update operation. I have attached the code snippet plus the Screenshots of my documents.
Let me know if I was able to make it clear enough for you to understand.




We cannot cut-n-paste your documents when you publish them as a screenshot.

We cannot cut-n-paste your code when you publish it as a screenshot.

We need text that

Please read Formatting code and log snippets in posts.

Documents are following:

{"_id":{"$oid":"61fb77d0b3185a7a67f34c64"},"team":[{"name":"Abid Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f3"},"isCaptain":false},{"name":"Abdullah Shafique","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f4"},"isCaptain":false},{"name":"Cameron Green","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94616"},"isCaptain":false},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Glenn Maxwell","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94618"},"isCaptain":false},{"name":"Josh Hazlewood","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94619"},"isCaptain":false},{"name":"Jhye Richardson","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461a"},"isCaptain":false},{"name":"Matthew Wade","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461b"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":true}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7799b3185a7a67f34bee"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}} 
{"_id":{"$oid":"61fb7e10b3185a7a67f34d9d"},"team":[{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}}
{"_id":{"$oid":"61fc2b8cb3185a7a67f3570f"},"team":[{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Mitchel Starc","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461f"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"1"}}
{"_id":{"$oid":"61ff4b38b3185a7a67f36712"},"team":[{"name":"Abid Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f3"},"isCaptain":false},{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":true},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Marnus Labuschagne","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461e"},"isCaptain":false},{"name":"Mohammad Rizwan","role":"wicketkeeper","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94606"},"isCaptain":false},{"name":"Shaheen Afridi","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9460c"},"isCaptain":false},{"name":"Hasan Ali","role":"bowler","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fe"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false},{"name":"Nathan Lyon","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94623"},"isCaptain":false},{"name":"Steven Smith","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94626"},"isCaptain":false},{"name":"Usman Khawaja","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94628"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61ff4a84b3185a7a67f3669c"},"matchid":{"$oid":"61fb7721b3185a7a67f34b78"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"0"}}
{"_id":{"$oid":"621b0135b3185a7a67f3784a"},"team":[{"name":"Babar Azam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f7"},"isCaptain":false},{"name":"Fakhar Zaman","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945f9"},"isCaptain":false},{"name":"Fawad Alam","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fb"},"isCaptain":false},{"name":"Haider Ali","role":"batsman","team":"Pakistan","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf945fc"},"isCaptain":true},{"name":"David Warner","role":"batsman","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94617"},"isCaptain":false},{"name":"Glenn Maxwell","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94618"},"isCaptain":false},{"name":"Matthew Wade","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf9461b"},"isCaptain":false},{"name":"Cameron Green","role":"allrounder","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94616"},"isCaptain":false},{"name":"Alex Carey","role":"wicketkeeper","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94615"},"isCaptain":false},{"name":"Scott Boland","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94625"},"isCaptain":false},{"name":"Pat Cummins","role":"bowler","team":"Australia","points":{"$numberInt":"0"},"_id":{"$oid":"61f62219b1c44a37bbf94624"},"isCaptain":false}],"tournmentslug":"pakistan-australia-2022","userid":{"$oid":"61fb7d86b3185a7a67f34d27"},"matchid":{"$oid":"61fb77fbb3185a7a67f34cde"},"totalPoints":{"$numberInt":"0"},"__v":{"$numberInt":"0"}}

Following is my code snippet

    const { id, points } = req.body;

    MyTeam.find({ 'team._id': id }).where('team.isCaptain').equals(true).updateMany(
        { 
            $set: {
                'team.$.points': points*2,
                'totalPoints': points*2
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )

    MyTeam.find({ 'team._id': id }).where('team.isCaptain').equals(false).updateMany(
        { 
            $set: {
                'team.$.points': points,
                'totalPoints': points
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )

Let me know if this is fine or if you want me to share the code and documents in other way.

I do not know the following syntax.

Is it mongoose or something like that?

You are using $set rather than $inc? If you do the operation for 2 people of the same team, your total point will be equal to the last value, not the sum of the 2 values. I am pretty sure that $inc is more appropriate.

You are doing 2 database access to do your update. You should always try to avoid doing 2 db access. In this case, you could use $cond to $inc (or $set) to a different value based on the isCaptain.

Something like

"totalPoints" : { "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] } 

I am surprise that you do that simply for "team._id":id, don’t you want to restrict the update to a given matchid? For example, Hasan Ali is present in 3 documents. Your query will update the 3 documents where Hasan Ali is listed.

Yeah, so I used $set instead of $inc just because of testing. I didn’t wanted to manually reset the values back to original (which is “0”) everytime I make the request to the endpoint since that’s quite time consuming.
Secondly, I was trying this (MyTeam.find({ ‘team._id’: id }).where(‘team.isCaptain’).equals(true)) code but I had another line of code before which is below

MyTeam.updateMany({ 'team._id': id }, {
 // Rest of the code is same as above
})

It works the same.
Now coming on to the code you shared, I tried it but I got the following error
‘’’
CastError: Cast to Number failed for value “{ ‘$multiply’: [ 10, { ‘$cond’: [Array] } ] }” (type Object) at path “totalPoints”
messageFormat: undefined,
stringValue: "{ '$multiply': [ 10, { '$cond': [Array] } ] }",
kind: ‘Number’,
value: {
‘$multiply’: [ 10, { ‘$cond’: [ ‘$isCaptain’, 2, 1 ] } ]
},
path: ‘totalPoints’,
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert.ok(!isNaN(val))

{
generatedMessage: true,
code: ‘ERR_ASSERTION’,
actual: false,
expected: true,
operator: ‘==’
},
valueType: ‘Object’
}
‘’’
I modified it in the following way but it is giving me a similar sort of error

 'team.$.points': Number({ "$cond": ["$isCaptain", points*2, points] }),
 'totalPoints': Number({ "$cond": ["$isCaptain", points*2, points] })

So, how can I fix this error? I just want to have the following implementation.
If isCaptain is true, I want ‘team.$.points’ and ‘totalPoints’ to be assigned or increment as points*2. And if isCaptain is false, I want ‘team.$.points’ and ‘totalPoints’ to be assigned or increment as simply points.

My overall code snippet looks like the following:

    MyTeam.updateMany({ 'team._id': id },
    // MyTeam.where('team._id').equals(id).$where('team.isCaptain').equals(false).updateMany(
        { 
            $inc: {
                'team.$.points':{ "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] },
                'totalPoints': { "$multiply" : [ points , { "$cond" : [ "$isCaptain" , 2 , 1 ] } ] }
            }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
    )

Smart move, just warn us before the next time.

The error seems to come from mongoose or an other framework rather than coming from mongod. However, I do what I propose directly in the shell, I do not get the error you get but I still don’t get the appropriate result.

You might need to use the new $set with aggregation with a $map to update the array. I still don’t know for sure.