我需要在此重命名 indentifier
:
{ "general" :
{ "files" :
{ "file" :
[
{ "version" :
{ "software_program" : "MonkeyPlus",
"indentifier" : "6.0.0"
}
}
]
}
}
}
我试过了
db.nrel.component.update(
{},
{ $rename: {
"general.files.file.$.version.indentifier" : "general.files.file.$.version.identifier"
} },
false, true
)
但它返回:$rename source 可能不是动态数组
。
最佳答案
对于它的值(value),虽然听起来很糟糕,但解决方案实际上非常简单。这当然取决于你有多少记录。但这是我的例子:
db.Setting.find({ 'Value.Tiers.0.AssetsUnderManagement': { $exists: 1 } }).snapshot().forEach(function(item)
{
for(i = 0; i != item.Value.Tiers.length; ++i)
{
item.Value.Tiers[i].Aum = item.Value.Tiers[i].AssetsUnderManagement;
delete item.Value.Tiers[i].AssetsUnderManagement;
}
db.Setting.update({_id: item._id}, item);
});
我遍历了我的集合,其中找到了数组并找到了“错误”的名称。然后我遍历子集合,设置新值,删除旧值,并更新整个文档。这是相对无痛的。当然,我只有几万行要搜索,其中只有几十行符合条件。
不过,我希望这个答案对某人有所帮助!
编辑:将 snapshot()
添加到查询中。在评论中查看原因。
You must apply
snapshot()
to the cursor before retrieving any documents from the database. You can only usesnapshot()
with unsharded collections.
从 MongoDB 3.4 中删除了 snapshot()
函数。所以如果使用 Mongo 3.4+,上面的例子应该去掉 snapshot()
函数。
https://stackoverflow.com/questions/9122966/
相关文章:
sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
sql - 为什么 MongoDB 比 SQL DB 快得多的任何详细和具体原因?
c# - MongoDB C# 驱动程序 : Ignore Property on Insert
node.js - 如何检查MongoDB native nodejs驱动程序中是否存在集合?
mongodb - $cond 中存在 $exists 的条件分组
java - MongoDB 是关系 db + lucene 的有效替代品吗?
mongodb - 如何在不丢失数据的情况下调整 mongodb 上限集合的大小?