Mongodb-$ set和$ setOnInsert中的重复字段 [英] Mongodb - duplicate fields in $set and $setOnInsert

查看:434
本文介绍了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中的重复字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆