Javascript 如何在MongoDB中高效地更新集合字段?
我把数据集作为Javascript 如何在MongoDB中高效地更新集合字段?,javascript,mongodb,time,Javascript,Mongodb,Time,我把数据集作为 {"id":1,"Timestamp":"Mon, 11 May 2015 07:57:46 GMT","brand":"a"} {"id":2,"Timestamp":"Mon, 11 May 2015 08:57:46 GMT","brand":"a"} 数据的预期结果是 {"id":1,"Timestamp":ISODate("2015-05-11T07:57:46Z"),"brand":"a"} {"id":2,"Timestamp":ISODate("2015-05-
{"id":1,"Timestamp":"Mon, 11 May 2015 07:57:46 GMT","brand":"a"}
{"id":2,"Timestamp":"Mon, 11 May 2015 08:57:46 GMT","brand":"a"}
数据的预期结果是
{"id":1,"Timestamp":ISODate("2015-05-11T07:57:46Z"),"brand":"a"}
{"id":2,"Timestamp":ISODate("2015-05-11T08:57:46Z"),"brand":"b"}
这意味着我要将每行中的时间戳从string修改为ISODate
我现在的代码是
db.tmpAll.find().forEach(
function (a) {
a.Timestamp = new Date(a.Timestamp);
db.tmpAll2.insert(a);
}
);
它运行成功,但运行代码需要几分钟,并且需要创建一个新集合。有什么有效的方法吗?您不需要创建新集合。使用该方法更新您的文档
db.tmpAll.find().forEach(function(doc){
doc.Timestamp = new Date(doc.Timestamp);
db.tmpAll.save(doc);
})