mongo的update $set和upsert: true有什么区别
我想问这个问题并回答它,因为我正在浏览 updateMany() 和 UpdateOne() 的 mongo 文档,作为初学者,我最初对使用 $set 运算符和“upsert: true”之间的区别感到困惑“ 选项。
I wanted to ask this question and answer it because I was going through the mongo documentation for updateMany() and UpdateOne(), and as a beginner, I was initially confused about the difference between using the $set operator and the "upsert: true" option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是文档所说的:
Mongo更新: $set
Mongo 更新:$upsert
答案:本质上的区别在于,如果更新查询找到文档但未找到指定的字段,$set 运算符将创建一个新的字段。如果更新查询未找到文档,则更新函数会被确认,但不会更新或创建任何内容。
选项如果查询未找到指定的文档,将创建一个新的文档。
如果查询结果匹配,$set 和 upsert 都将执行更新。
This is what the documentation says:
Mongo Update: $set
Mongo Update: $upsert
ANSWER: Essentially the difference is that the $set operator will create a new field if the update query finds the document but not the field specified. If the update query doesn't find the document, the update function is acknowledged but nothing is updated or created.
The option <upsert: true> will create a new document if the query doesn't find the the document specified.
Both $set and upsert will perform an update if the query results in a match.