本文介绍了Mongodb-$ set和$ setOnInsert中的重复字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 帖子,接受的答案说明您在upsert操作中的$set
和$setOnInsert
下不能有相同的字段.
In this post, the accepted answer explains that you cannot have the same fields under $set
and $setOnInsert
in an upsert operation.
有人可以解释为什么会这样吗?似乎$setOnInsert
不应与$set
冲突,因为前者在插入文档时使用,而后者在更新文档时使用.
Can someone explain why this is? It seems like the $setOnInsert
shouldn't conflict with $set
, since the former is used when a document is inserted, and the latter is used when the document is updated.
推荐答案
$ set 运算符也用于 upsert .因此,在 $ set 和 $ setOnInsert 上引用相同的字段是没有意义的.
$set operator is used on upsert too. So it's nonsense to refer same fields both on $set and $setOnInsert.
只需在一个空集合上尝试一下:
Just try this on an empty collection:
db.items.remove();
db.items.update({},{$set:{a:1},$setOnInsert:{b:2}},{upsert:1})
db.items.find({});
这篇关于Mongodb-$ set和$ setOnInsert中的重复字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!