我需要sid在大约500K文档的集合中为每个文档创建一个新字段.每个sid都是独一无二的,并基于该记录的现有roundedDate和stream字段.
我正在使用以下代码:
var cursor = db.getCollection('snapshots').find();
var iterated = 0;
var updated = 0;
while (cursor.hasNext()) {
var doc = cursor.next();
if (doc.stream && doc.roundedDate && !doc.sid) {
db.getCollection('snapshots').update({ "_id": doc['_id'] }, {
$set: {
sid: doc.stream.valueOf() + '-' + doc.roundedDate,
}
});
updated++;
}
iterated++;
};
print('total ' + cursor.count() + ' iterated through ' + iterated + ' updated ' + updated);
Run Code Online (Sandbox Code Playgroud)
它起初效果很好,但几个小时后大约有100K记录,它出错了:
Error: getMore command failed: {
"ok" : …Run Code Online (Sandbox Code Playgroud)