我必須從 Merchants 集合中更新“AcquireMerchant.Gateway”列,但要獲得我必須更新的確切記錄數量,需要對 Banks 集合和 Institution 集合進行兩次查找。
這是我正在處理但不起作用的查詢。
問題是當它執行 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"}}));
uj5u.com熱心網友回復:
聚合回傳具有 3 個欄位的檔案(您明確洗掉了 _id):
"Institutions.GlobalId"
"Merchants_INFRADB1230.AcquireMerchant.Gateway"
"Merchants_INFRADB1230.MID"
因此,在 中forEach,doc物件將只有那些欄位,并且doc._id將是未定義的{_id:doc._id},如果允許未定義,這將使過濾器不匹配任何內容。
該欄位"Merchants_INFRADB1230"是在聚合中創建的,因此不存在于集合中的檔案中,因此更新操作應該參考該欄位,因為它存在于這些檔案中:
{ $set: { "AcquireMerchant.Gateway": "Update2"}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329211.html
