我需要更新Merchants集合中的“AcquireMerchant.Gateway”列,但为了获得我需要更新的确切记录数量,需要对Banks集合和Institution集合进行两次查询。
这是我正在处理的查询,但它无法工作。
问题出在执行forEach时,会出现“无法与未定义的内容进行比较”的错误。聚合操作运行完美。
这是我正在处理的查询,但它无法工作。
问题出在执行forEach时,会出现“无法与未定义的内容进行比较”的错误。聚合操作运行完美。
db.getCollection("Merchants_INFRADB1230").aggregate(
[
{
"$project": {
"Merchants_INFRADB1230": "$$ROOT",
"_id": 0
}
},
{
"$lookup": {
"as": "Bancos",
"foreignField": "_id",
"from": "Bancos",
"localField": "Merchants_INFRADB1230.SellingBankId"
}
},
{
"$unwind": {
"path": "$Bancos",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"as": "Instituciones",
"foreignField": "_id",
"from": "Instituciones",
"localField": "Bancos.InstitucionesId"
}
},
{
"$unwind": {
"path": "$Instituciones",
"preserveNullAndEmptyArrays": true
}
},
{
"$match": {
"Instituciones.GlobalId": "4fb8bc86af95",
"Merchants_INFRADB1230.AcquireMerchant.Gateway": "Update1"
}
},
{
"$project": {
"Instituciones.GlobalId": 1,
"Merchants_INFRADB1230.AcquireMerchant.Gateway": 1,
"Merchants_INFRADB1230.MID": 1
}
}
]).forEach(doc => db.Merchants_INFRADB1230.updateMany(
{ _id: doc._id },
{ $set: { "Merchants_INFRADB1230.AcquireMerchant.Gateway": "Update2"}}));
db.Merchants_INFRADB1230
,它是一个集合吗?为什么你在上面的操作中使用了不同的方式?试试db.getCollection("Merchants_INFRADB1230")
,同时确保doc._id
没有问题。 - Takis