Cannot add array using string field name [$] while updating array fields

rows I am trying to update mongodb for each field in an array of records.

The following is an example diagram:

{ "_id" : ObjectId("508710f16dc636ec07000022"), "summary" : "", "uid" : "ABCDEF", "username" : "bigcheese", "name" : "Name of this document", "status_id" : 0, "rows" : [ { "score" : 12, "status_id" : 0, "uid" : 1 }, { "score" : 51, "status_id" : 0, "uid" : 2 } ] } 

So far, I have been able to perform one update:

 db.mycollection.update({"uid":"ABCDEF","rows.uid":1}, {$set:{"rows.$.status_id":1}},false,false) 

However, I am struggling with how to perform an update that will update all array entries to status_id 1 (for example).

Below I will tell you how it should work:

 db.mycollection.update({"uid":"ABCDEF"}, {$set:{"rows.$.status_id":1}},false,true) 

However, I get the error message:

cannot join array using string field name [$]

I tried for quite some time, no luck. Any pointers?

+6
source share
1 answer

You cannot do such a β€œwildcard” update of the array elements you are looking for. I think the best you can do is set each status_id element status_id value like this:

 db.mycollection.update( {"uid":"ABCDEF"}, {$set:{ "rows.0.status_id":1, "rows.1.status_id":1 }}, false, true); 
+6
source