How would the following query change if we set { upsert: true } as the third parameter? db.sample.updateMany( { a: 4 }, { $set: { b: 2, d: -4 } } ) Check all that apply:
题目选项
- A.The upsert will both insert and update a document.
- B.The upsert will insert a document if no documents exist where "a" is equal to 4.
- C.An upsert will remove a document, if found, before inserting a new document.
答案置信度:高
正确答案:B
解析语言:中文
upsert 的语义是"匹配不到就插入":当没有 a=4 的文档时,MongoDB 把查询条件与 $set 的内容合成一条新文档插入;若匹配到了则只做更新,不会既插又更新(A 错),也不会先删后插(C 错)。还要注意 updateMany+upsert 在无匹配时也最多只插入一条文档。