问题描述
我知道这可能是一个愚蠢的问题,但我在一本电子书中读到,MongoDB 插入中有一个 upsert
选项.我找不到关于此的适当文档.有人可以教育我吗?
I know this may be a silly question, but I read on an e-book that there is an upsert
option in MongoDB insert. I couldn't find proper documentation about this. Can someone educate me about this?
推荐答案
因为 upsert
被定义为当没有文档符合查询条件时创建一个新文档" insert
命令中没有 upserts
的地方.它是 update
命令的一个选项.如果您执行如下命令,它将作为 update
、如果有匹配query
的文档,或者作为insert
以update
描述的文档作为参数一>.
Since upsert
is defined as operation that "creates a new document when no document matches the query criteria" there is no place for upserts
in insert
command. It is an option for the update
command. If you execute command like below it works as an update
, if there is a document matching query
, or as an insert
with document described by update
as an argument.
db.collection.update(query, update, {upsert: true})
MongoDB 3.2 添加了 replaceOne
一个>:
MongoDB 3.2 adds replaceOne
:
db.collection.replaceOne(query, replacement, {upsert: true})
具有类似行为,但其 replacement
不能包含更新操作符.
which has similar behavior, but its replacement
cannot contain update operators.
这篇关于有没有“upsert"mongodb 插入命令中的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!