javascript - Aggregate is not a function - Mongoose Nodejs - Stack Overflow

Can somebody help me to fix this, here is my code for aggregate from mongoose:export class GetVehiclesb

Can somebody help me to fix this, here is my code for aggregate from 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));
        });
    });
}}

And I get an error like this on the console:

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

DONE, I Thanks for Hana :) And i change my mitraId type ObjectId mitraId : { type: Schema.Types.ObjectId, required: true },

Can somebody help me to fix this, here is my code for aggregate from 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));
        });
    });
}}

And I get an error like this on the console:

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

DONE, I Thanks for Hana :) And i change my mitraId type ObjectId mitraId : { type: Schema.Types.ObjectId, required: true },

Share edited Jan 9, 2018 at 6:43 KARTHIKEYAN.A 20.2k10 gold badges137 silver badges150 bronze badges asked Oct 4, 2017 at 0:40 Galang ArbiSGalang ArbiS 831 gold badge3 silver badges11 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 1

Try to avoid find, populate, lean function here and follow as like below

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));
        });
    });
}}

You can use $lookup in the aggregation statement instead of using find, and populate here.

Like this:

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

I hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043910a4607977.html

相关推荐

  • javascript - Aggregate is not a function - Mongoose Nodejs - Stack Overflow

    Can somebody help me to fix this, here is my code for aggregate from mongoose:export class GetVehiclesb

    8小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信