For a document structure is:
{
"_id" : ObjectId("57133995fb5f8930d0e9b81a"),
"taskList" : [{
"taskId" : NumberLong(1),
"state" : "ST00",
"createTime" : ISODate("2016-04-17T07:21:58.424Z")
},{
"taskId" : NumberLong(2),
"state" : "ST00",
"createTime" : ISODate("2016-04-17T07:21:58.424Z")
},{
"taskId" : NumberLong(3),
"state" : "ST00",
"createTime" : ISODate("2016-04-17T07:21:58.424Z")
}]
}
TaskList length is uncertain.
How to modify the contents of the array in batches?
For example, batch modification “_ id”: the state field of the taskList embedded element of objectid (“57133995fb5f8930d0e9b81a”) is’ ST02′
MongoDB’s update statement can only update the first matching element in the array at a time. Several thoughts:
1) Re-model, put tasklist into another table, and then use reference to reference. If you have many such needs
2) First make a query to get the length of taskList, and then spell update statement according to this length:
var length = db.test.aggregate([{$project:{lenOfArray: {$size:”$taskList”}}}]).next().lenOfArray;
var updateObj = {};
for(var i=0; i<length; i++){
updateObj[“taskList.”+ i+”.stat”] = “ST02”;
}
db.test.update({ }, {$set: updateObj } );