Mar*_*.io 4 mongodb node.js mongodb-query
我在一个集合中的日期插入不正确,并且是一个简单的"2015-09-10" 字符串格式.
我想更新它们以更正ISO日期格式.
我已尝试使用Mongo进行循环,forEach()但我对如何更新集合中的每个文档都不太了解shell.
到目前为止,我正是在这一点上:
db.getCollection('schedules').find({}).forEach(function (doc) {
doc.time = new Date( doc.time ).toUTCString();
printjson( doc.time );
// ^ This just prints "Invalid Date"
// Also none of the below work when I try saving them
//doc.save();
//db.getCollection('schedules').save(doc);
});
Run Code Online (Sandbox Code Playgroud)
这里缺少什么?
执行此操作的最佳方法是使用"批量"操作
var collection = db.getCollection('schedules');
var bulkOp = collection.initializeOrderedBulkOp();
var count = 0;
collection.find().forEach(function(doc) {
bulkOp.find({ '_id': doc._id }).updateOne({
'$set': { 'time': new Date(doc.time) }
});
count++;
if(count % 100 === 0) {
// Execute per 100 operations and re-init
bulkOp.execute();
bulkOp = collection.initializeOrderedBulkOp();
}
});
// Clean up queues
if(count > 0) {
bulkOp.execute();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3043 次 |
| 最近记录: |