如何使用查找相同模型但两个聚合操作

问题描述 投票:0回答:0

我尝试检查 if else 条件然后尝试查找数据,首先我使用一种聚合方法而不是我从这个聚合中收集 id 然后再次尝试使用查找这个 id 但数据没有来

//const { default: mongoose } = require("mongoose");  const GetOrderBySingleSellerService = async (req, model) =\> {   try {     let ID = mongoose.Types.ObjectId(req.params.id);     let data = await model.aggregate(\[       {         $match: {           sellerID: ID,         },       },       {         $lookup: {           from: "become-a-sellers",           localField: "sellerID",           foreignField: "\_id",           as: "orderinfo",           pipeline:\[             {               $project:{                 sellerNationalityID:1,                 sellerProfilePhoto:1,                 kitchenImages:1,                 kitchenName:1,                 sellerNationality:1,                 sellerNationalityByBorn:1               }             }           \]         },       },       {         $lookup:{           from:"users",           localField:"customerID",           foreignField:"\_id",           as:"userInfo",           pipeline:\[             {               $project:{                 userFullName:1,                 userName:1,                 userEmail:1,                 userMobileNo:1,                 userPhoto:1,                 zipCode:1                }             }           \]         }       },       {         $lookup: {           from: "addressbooks",           localField: "orderDeliveryAddressID",           foreignField: "\_id",           as: "deliveryAddress",           pipeline: \[             {               $project: {                 cityID: 1,                 regionID: 1,                 countryID: 1,                 // addressText: 1,                 // addressType: 1,                 // shippingDefault: 1,                 // billingDefault: 1,                 // latitude: 1,                 // logitude: 1,                 // zipCode: 1,                 // status: 1,                 // deliveryAddress:0                              },             },           \],         },       },       {         $lookup: {           from: "addressbooks",           localField: "orderBillingAddressID",           foreignField: "\_id",           as: "billingAddress",           pipeline: \[             {               $project: {                 cityID: 1,                 regionID: 1,                 countryID: 1,                 // addressText: 1,                 // addressType: 1,                 // shippingDefault: 1,                 // billingDefault: 1,                 // latitude: 1,                 // logitude: 1,                 // zipCode: 1,                 // status: 1                },             },           \],         },       },       {         $lookup:{         from: "orderstatuses",         localField: "orderStatusID",         foreignField: "\_id",         as: "orderStatus",                }       },       {         $lookup:{           from:"citys",           localField :"deliveryAddress.cityID",           foreignField:"\_id",           as:"deliveryAddressCity",           pipeline:\[             {               $project:{                 cityName:1               }             }           \]         }       },       {         $lookup:{           from:"citys",           localField :"billingAddress.cityID",           foreignField:"\_id",           as:"billingAddressCity",           pipeline:\[             {               $project:{                 cityName:1               }             }           \]         }       },       {         $lookup:{           from:"countries",           localField: "deliveryAddress.countryID",           foreignField:"\_id",           as:"deliveryAddressCountry",           pipeline:\[             {               $project:{                 countryName:1,                 countryCode:1,                 countryCapital:1               }             }           \]         }       },       {         $lookup:{           from:"countries",           localField: "billingAddress.countryID",           foreignField:"\_id",           as:"billingAddressCountry",           pipeline:\[             {               $project:{                 countryName:1,                 countryCode:1,                 countryCapital:1               }             }           \]         }       },       {         $lookup:{           from:"regionstates",           localField:"deliveryAddress.regionID",           foreignField:"\_id",           as:"deliveryAddressRegion",           pipeline:\[             {               $project:{                 regionName:1               }             }           \]         }       },       {        $lookup:{         from:"regionstates",         localField:"billingAddress.regionID",         foreignField:"\_id",         as:"billingAddressRegion",         pipeline:\[           {             $project:{               regionName:1             }           }         \]        }        }      \]); if(data\[0\].deliveryAddress\[0\].cityID=="63e8b32a798c4de2d1ba6c3d") {   // hare not return lookup data    let data1 = await model.aggregate(\[     {             $lookup:{         from:"citys",         localField :"data\[0\].deliveryAddress\[0\].cityID",         foreignField:"\_id",         as:"deliveryAddressCity",         pipeline:\[           {             $project:{               cityName:1             }           }         \]     }   }    \])   console.log("from data1",data1) } else{   console.log("no"); }     return { status: "Success", data: data };   } catch (e) {     return { status: "Fail", data: e };   } }; module.exports = GetOrderBySingleSellerService;
mongodb express mongoose aggregate
© www.soinside.com 2019 - 2024. All rights reserved.