簡體   English   中英

如何在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.save方法更新您的文檔。

db.tmpAll.find().forEach(function(doc){
    doc.Timestamp = new Date(doc.Timestamp); 
    db.tmpAll.save(doc); 
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM