Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个收藏:{"_id":1,"a":3,"b":5,“a+b”:0}
{"_id":1,"a":3,"b":5,“a+b”:0}
我想做这个:
y=db.collection.find({"_id":1},{"a":1,"b":1}) sum=y["a"]+y["b"] db.collection.update({"_id":1},{"$set":{"a+b",sum}})
所以集合将是:{"_id":1,"a":3,"b":5,"a+b":8}
{"_id":1,"a":3,"b":5,"a+b":8}
我可以将这两个运算符组合成一个 sql 命令吗?
不,您不能在一个命令中执行此操作,因为 mongodb 不支持更新中的字段引用。