[英]Update a field in mongodb collection even if it doesnt exist
I want to set isChecked
field to mongodb collection even if this field does not exist in the collection: 我想将isChecked
字段设置为mongodb集合,即使集合中不存在此字段:
User.updateOne({ id }, { $set: { isChecked: true } });
But it doesnt update because isChecked
does not exist. 但它没有更新,因为isChecked
不存在。 I remember it was something with new
but I dont remember exactly. 我记得它是new
东西,但我不记得确切。
PS I love mongodb but I hate it's documentation. PS我喜欢mongodb,但我讨厌它的文档。 0 readability 0可读性
The behavior of this query should not depend on if isChecked
exists; 此查询的行为不应取决于isChecked
存在; the $set
operator will set the value of isChecked
, regardless of if the field exists in the document before or not : $set
运算符将设置isChecked
的值, 无论该字段之前是否存在该字段 :
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. 如果该字段不存在,$ set将添加具有指定值的新字段,前提是新字段不违反类型约束。
Were you possibly thinking of upsert
or some other behavior with incrementing? 您是否可能考虑使用递增的upsert
或其他一些行为?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.