如何更新 MongoDB 中有效的集合字段?
How to update a field of collection efficient in MongoDB?
我有数据集
{"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"}
这意味着我想将每行中的时间戳从字符串修改为ISODate
我当前的代码是
db.tmpAll.find().forEach(
function (a) {
a.Timestamp = new Date(a.Timestamp);
db.tmpAll2.insert(a);
}
);
它 运行 成功了,但是 运行 代码需要几分钟,并且需要创建一个新的集合。有什么有效的方法吗?
您不需要创建新的 collection。使用 collection.save
方法更新您的文档。
db.tmpAll.find().forEach(function(doc){
doc.Timestamp = new Date(doc.Timestamp);
db.tmpAll.save(doc);
})
我有数据集
{"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"}
这意味着我想将每行中的时间戳从字符串修改为ISODate 我当前的代码是
db.tmpAll.find().forEach(
function (a) {
a.Timestamp = new Date(a.Timestamp);
db.tmpAll2.insert(a);
}
);
它 运行 成功了,但是 运行 代码需要几分钟,并且需要创建一个新的集合。有什么有效的方法吗?
您不需要创建新的 collection。使用 collection.save
方法更新您的文档。
db.tmpAll.find().forEach(function(doc){
doc.Timestamp = new Date(doc.Timestamp);
db.tmpAll.save(doc);
})