Mongo operator position with $elemMatchthe problem;
The $ operator can update the first element of an array that matches several query criteria specified using the $ elemMatch () operator.
, , mongo, . rows.aac , status:1 row.aac, , :
db.collectionName.update({
"_id": 5,
"rows": {
"$elemMatch": {
"id": "abc"
}
}
}, {
$set: {
"rows.$.status": 1
}
}, true, false)
mongo , upsert multi.
, programming code script. , cursor forEach:
db.collectionName.find().forEach(function(data) {
for (var ii = 0; ii < data.rows.length; ii++) {
db.collectionName.update({
"_id": data._id,
"rows.id": data.rows[ii].id
}, {
"$set": {
"rows.$.status": 1
}
}, true, false);
}
})
, mongo, , mongo bulk:
var bulk = db.collectionName.initializeOrderedBulkOp();
var counter = 0;
db.collectionName.find().forEach(function(data) {
for (var ii = 0; ii < data.rows.length; ii++) {
var updatedDocument = {
"$set": {}
};
var setStatus = "rows." + ii + ".status";
updatedDocument["$set"][setStatus] = 101;
bulk.find({
"_id": data._id
}).update(updatedDocument);
counter++;
if (counter % 1000 == 0) {
bulk.execute();
bulk = db.collectionName.initializeOrderedBulkOp();
}
}
});
if (counter % 1000 != 0)
bulk.execute();