我试图运行一个mongo查询来更新一个字段的值与另一个字段的值。我有以下文件:mongodb更新字段a是字段b的值
{ "_id" : ObjectId("56e0a3a2d59feaa43fba49d5"), "old" : 16, "new" : 17 }
{ "_id" : ObjectId("56e0a3a2d59feaa43fba49d3"), "old" : 11, "new" : 12 }
我想使它看起来像此更新后:
{ "_id" : ObjectId("56e0a3a2d59feaa43fba49d5"), "old" : 16, "new" : 16 }
{ "_id" : ObjectId("56e0a3a2d59feaa43fba49d3"), "old" : 11, "new" : 11 }
我已经试过,没有运气以下
db.runCommand(
{
findAndModify: "testData",
query: { $where: "this.new != this.old" },
update: { old : this.new },
upsert: true
}
)
和
db.testData.update({ $where: "this.new != this.old" }, { $set: { old: this.new } });
这是偶数po可以用mongoDb吗?
我想在单个查询中执行此操作,而不是遍历每个文档。
任何想法将不胜感激。
谢谢
感谢您回应 – user583576
不客气,NB我添加的信息(以防万一) 。 – Ilya
使用聚合 - 请参阅我的回答 – profesor79