Consider the following update command: var updateDocument = ... db.books.updateOne({ "title": "Catch-22" }, updateDocument) Select which value for updateDocument will add multiple individual review sub-documents to the reviews array field: Check all that apply:
题目选项
- A.{ "$push": { "reviews": [ { "rating": 1, "text": "review 1" }, { "rating": 2, "text": "review 2" } ] } }
- B.{ "$inc": { "reviews": { "$each": [ { "rating": 1, "text": "review 1" }, { "rating": 2, "text": "review 2" } ] } } }
- C.{ "$push": { "reviews": { "$each": [ { "rating": 1, "text": "review 1" }, { "rating": 2, "text": "review 2" } ] } } }
答案置信度:高
正确答案:C
解析语言:中文
要向数组字段一次追加"多个独立子文档",必须用 $push 搭配 $each:$each 会把数组中的每个元素逐个 push 进去(C)。A 直接把数组字面量交给 $push,会把整个数组当成一个嵌套数组元素插入;B 的 $inc 是数值自增操作符,不接受 $each,语法本身无效。