2012-12-19 32 views
1

我想將我的數據從「demographic.education」列移動到「demographic.school」。我怎麼能這樣做?如何在MongoDB中將數據從一列移動到另一列?

例如:

db.users.update({"demographic.education":{$exists: true, $ne: ""}}, {$set: {"demographic.school":demographic.education}}) 
+1

你的意思複製值(離開'demographic.education'原樣)或重命名密鑰? – JohnnyHK

+0

重命名密鑰。但是,如果我想複製這些值,它會是什麼樣子? – Kamilski81

+1

這並不容易。基本上你必須遍歷集合並逐個修改每個文檔。 – JohnnyHK

回答

2

您可以使用此$rename修改:

db.users.update({"demographic.education": {$exists: true, $ne: ""}}, {$rename: {"demographic.education": "demographic.school"}}) 

Documentation

+0

如果我想複製這些值而不是重命名,它會是什麼樣子?僅供將來使用。 – Kamilski81

相關問題