簡體   English   中英

Mongo DB:對象模式更改對文檔設計的影響

[英]Mongo DB : Impact of object schema changes on document design

如何在Mongo數據庫中處理模式更改,例如在重構后更改對象模式設計並影響文檔模式。 有沒有辦法更新文檔架構?

您可以在整個架構上運行更新,刪除字段,或添加字段並將其設置為計算值,如果這是您正在獲取的內容。

假設你有一個x字段,並且你想添加一個應該設置為x / 2的字段,你可以這樣做:

PRIMARY> db.test.insert({x:15});
PRIMARY> db.test.insert({x:30});
PRIMARY> db.test.insert({x:50}); 
PRIMARY> db.test.find();
{ "_id" : ObjectId("4f9df1ebed2b924eedb8cad9"), "x" : 15 }
{ "_id" : ObjectId("4f9df1eeed2b924eedb8cada"), "x" : 30 }
{ "_id" : ObjectId("4f9df1f1ed2b924eedb8cadb"), "x" : 50 }
PRIMARY> db.test.find().forEach(function(doc) { 
            doc.y = doc.x/2; 
            db.test.save(doc); 
         });
PRIMARY> db.test.find();
{ "_id" : ObjectId("4f9df1ebed2b924eedb8cad9"), "x" : 15, "y" : 7.5 }
{ "_id" : ObjectId("4f9df1eeed2b924eedb8cada"), "x" : 30, "y" : 15 }
{ "_id" : ObjectId("4f9df1f1ed2b924eedb8cadb"), "x" : 50, "y" : 25 }

暫無
暫無

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

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