在MongoDB缝合功能中,我具有以下功能。
exports = function(){ //orderId
let orderId = "2A01291D-0CAB-448E-B82D-17235526B55D"
//let paid = true
console.log("triggered the update session paid status function")
var sessionCol = context.services.get("mongodb-atlas").db("ClimbingWall").collection("sessions");
const query = {"customers.orderId":orderId};
var update = {"$set":{"customers.$[].paid": true}}
return sessionCol.updateMany(query, update)
.then(result => {
const { matchedCount, modifiedCount } = result;
console.log(`Successfully matched ${matchedCount} and modified ${modifiedCount} items.`)
return result
})
.catch(err => console.error(`Failed to update items: ${err}`))
};
该函数在另一个文档内的文档数组中搜索与orderId相匹配的文档,并将付费字段更新为true。但是,此刻将更新数组中的所有文档,而不仅仅是那些与orderId匹配的文档。 那么,如何只更新那些与查询匹配的内容呢?