【问题标题】:Aggregate is not a function - Mongoose Nodejs聚合不是函数 - Mongoose Nodejs
【发布时间】:2018-01-09 14:43:02
【问题描述】:

谁能帮我解决这个问题,这是我从 mongoose 聚合的代码:

export class GetVehiclesbyKotaCommandHandler {
constructor(namaKota) {
    return new Promise((resolve, reject) => {
        VehiclesDB.find().populate({
            path: 'mitraId',
            model: 'RentalDB',
            select: 'namaKota'
        }).aggregate([
            {
                $match : {
                    namaKota:namaKota
                }
            }
            ]).lean().then((dataVehicles)=>{
            if(dataVehicles !== null){
                resolve(dataVehicles);
            } else {
                reject (new NotFoundException('Couldn\'t find any Vehicles with namaKota' + namaKota));
            }
        }).catch((errDataVehicles)=>{
            reject(new CanNotGetVehiclesException(errDataVehicles.message));
        });
    });
}}

我在控制台上收到这样的错误:

TypeError: _VehiclesDB2.default.find(...).populate(...).aggregate is not a function

完成,感谢 Hana :) 我改变了我的 mitraId 类型 ObjectId mitraId:{ 类型:Schema.Types.ObjectId, 要求:真 },

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    尽量避免在此处使用查找、填充、精简功能,并按照如下方式进行操作

    export class GetVehiclesbyKotaCommandHandler {
    constructor(namaKota) {
        return new Promise((resolve, reject) => {
            VehiclesDB.aggregate([
                {
                  $lookup: {
                    from: 'RentalDB',
                    localField: 'mitraId',
                    foreignField: '_id',
                    as: 'mitra'
                  }
                }, {
                  $unwind: "$mitra"
                }, {
                  $match: {
                    "mitra.namaKota": namaKota
                  }
                }
              ]).then((dataVehicles)=>{
                if(dataVehicles !== null){
                    resolve(dataVehicles);
                } else {
                    reject (new NotFoundException('Couldn\'t find any Vehicles with namaKota' + namaKota));
                }
            }).catch((errDataVehicles)=>{
                reject(new CanNotGetVehiclesException(errDataVehicles.message));
            });
        });
    }}
    

    【讨论】:

      【解决方案2】:

      您可以在聚合语句中使用$lookup,而不是在此处使用findpopulate

      像这样:

      VehiclesDB.aggregate([
          {
            $lookup: {
              from: 'RentalDB',
              localField: 'mitraId',
              foreignField: '_id',
              as: 'mitra'
            }
          }, {
            $unwind: "$mitra"
          }, {
            $match: {
              "mitra.namaKota": namaKota
            }
          }
        ])
      

      我希望这会有所帮助。

      【讨论】:

      • 我尝试使用您的回答代码进行改进,它成功而不是错误,但我的回复中没有任何数据
      • 这里是我的 exec ===>>> .exec((err, dataVehicles)=>{ if(err) reject (new NotFoundException('Couldn\'t find any Vehicles with id' + namaKota )); 解决(dataVehicles); });
      • 嗨@GalangArbiS,您能否在您的问题中添加一些数据示例? :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2021-08-28
      • 1970-01-01
      • 2019-02-11
      • 2017-12-10
      • 2017-08-20
      • 2015-04-08
      相关资源
      最近更新 更多