我想将 isChecked
字段设置为 mongodb 集合,即使集合中不存在该字段:
User.updateOne({ id }, { $set: { isChecked: true } });
但它不会更新,因为 isChecked
不存在。我记得是 new
的东西,但我记不太清了。
附言我喜欢 mongodb 但我讨厌它的文档。 0可读性
最佳答案
此查询的行为不应依赖于 isChecked
是否存在; $set
运算符将设置 isChecked
的值,regardless of if the field exists in the document before or not :
If the field does not exist, $set will add a new field with the specified value, provided that the new field does not violate a type constraint.
您可能会想到 upsert
或其他一些递增的行为?
关于javascript - 更新 mongodb 集合中的字段,即使它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56247503/