Can somebody help me to fix this, here is my code for aggregate from mongoose:
有人可以帮我解决这个问题,这是我的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 },
完成,我感谢Hana :)我改变了我的mitraId类型ObjectId mitraId:{type:Schema.Types.ObjectId,required:true},
2 个解决方案
#1
0
You can use $lookup
in the aggregation statement instead of using find
, and populate
here.
您可以在聚合语句中使用$ lookup而不是使用find,并在此处填充。
Like this:
喜欢这个:
VehiclesDB.aggregate([
{
$lookup: {
from: 'RentalDB',
localField: 'mitraId',
foreignField: '_id',
as: 'mitra'
}
}, {
$unwind: "$mitra"
}, {
$match: {
"mitra.namaKota": namaKota
}
}
])
I hope this helps.
我希望这有帮助。
#2
0
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));
});
});
}}
#1
0
You can use $lookup
in the aggregation statement instead of using find
, and populate
here.
您可以在聚合语句中使用$ lookup而不是使用find,并在此处填充。
Like this:
喜欢这个:
VehiclesDB.aggregate([
{
$lookup: {
from: 'RentalDB',
localField: 'mitraId',
foreignField: '_id',
as: 'mitra'
}
}, {
$unwind: "$mitra"
}, {
$match: {
"mitra.namaKota": namaKota
}
}
])
I hope this helps.
我希望这有帮助。
#2
0
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));
});
});
}}