我知道这可能是一个愚蠢的问题,但我在一本电子书中读到,MongoDB 插入中有一个 upsert
选项。我找不到有关此的适当文档。有人可以教育我吗?
最佳答案
由于 upsert
被定义为“当没有文档匹配查询条件时创建一个新文档”的操作,所以 upserts
没有位置在 insert
命令中。它是 update
命令的一个选项。如果您执行如下命令,它将作为 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
:
db.collection.replaceOne(query, replacement, {upsert: true})
具有类似行为,但其替换
不能包含更新运算符。
关于mongodb - mongodb insert 命令中是否有 "upsert"选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974216/