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.
如果字段已经存在,如何更新文档中的字段,否则更新字段的值?$set 在该字段已经存在的文档上更新得很好。刚刚发现很多提示如何将新字段插入到整个集合中,但是如何在文档的基础上解决这个问题?
问候,克里斯
$set将添加一个或多个指定字段,如果它们在此文档中不存在,或者如果它们已经存在,则替换指定字段的现有值。
在更新语句中使用upsert标志。