I am trying to update an array as specified on this MongoDB official tutorial link. The Steps that I have followed is:
Create collection name test:
Insert document in collection:
db.getCollection('test').insert({"_id" : 2,"grades" : [{ "grade" : 90, "mean" : 75, "std" : 6 },{ "grade" : 87, "mean" : 90, "std" : 3 },{ "grade" : 85, "mean" : 85, "std" : 4 }]});
db.getCollection('test').insert({"_id" : 1,"grades" : [{ "grade" : 80, "mean" : 75, "std" : 6 },{ "grade" : 85, "mean" : 90, "std" : 4 },{ "grade" : 85, "mean" : 85, "std" : 6 }]});
Trying to update an array as specified in a tutorial:
db.getCollection('test').update( { }, { $inc: { "grades.$[].std" : -2 } }, { multi: true } )
As per tutorial, it must update the document. But, instead I receives an error:
cannot use the part (grades of grades.$[].std) to traverse the element ({grades: [ { grade: 80.0, mean: 75.0, std: 6.0 }, { grade: 85.0, mean: 90.0, std: 4.0 }, { grade: 85.0, mean: 85.0, std: 6.0 } ]})
I am using MongoDB 3.4 Does anyone know the solution?