【发布时间】:2015-02-16 02:39:37
【问题描述】:
假设我有一个字段为state 的对象,我想更新此字段,同时将state 的先前值保留在previous_state 字段中。首先,我尝试使用 unset-rename-set 进行更新:
collection.update(query, {$unset: {previous_state: ""}, $rename: {state: "previous_state"}, $set: {state: value}})
毫不奇怪,它不起作用。阅读后:
- Update MongoDB field using value of another field
- MongoDB update: Generate new field based on existing field, or update in place
- Update field with another field's value in the document
我几乎确信我没有在单个查询中执行此操作的解决方案。那么问题是什么是最佳实践呢?
【问题讨论】:
-
多个用户可以访问数据库吗?还是您只有一个连接。
-
我有一个作者和多个读者用户为这个集合。有什么区别吗?
-
这取决于你如何实现新状态的价值。但既然你只有一位作家,Lombric 的答案将是最好的。
-
这有什么更新吗?我同意@InancGumus
标签: mongodb mongodb-query