我在数组中得到一个空对象-我期待一个空数组。
我看过:When MongoDB aggregate can't find any result, it returns an array with an empty object 并尝试了各种$ cond方式,但始终在查询中获得“无法识别的参数$ cond
这是我的原始查询
const query = [
{
$match: { '_id': mongoose.Types.ObjectId(user_id) }
},
{
$lookup: {
from: 'cars',
localField: 'car_id',
foreignField: '_id',
as: 'users_car'
}
},
{
$unwind: {
path: '$users_car',
preserveNullAndEmptyArrays: true
}
},
{
$project: {
_id: '$users_car._id',
make: '$users_car.make',
model: '$users_car.model',
year: '$users_car.year',
color: '$users_car.color',
}
}
];
我尝试过的事情:
{
$unwind: {
path: '$users_sponsor',
preserveNullAndEmptyArrays: true
}
},
{
$cond: [
{ $gt: ['$users_car',0]},
{
$project: {
_id: '$users_car._id',
make: '$users_car.make',
model: '$users_car.model',
year: '$users_car.year',
color: '$users_car.color',
}
},
$$REMOVE
]
}
然后这个
{
$unwind: {
path: '$users_sponsor',
preserveNullAndEmptyArrays: true
}
},
{
$cond: [
{ $gt: ['$users_car',0]},
{
_id: '$users_car._id',
make: '$users_car.make',
model: '$users_car.model',
year: '$users_car.year',
color: '$users_car.color',
},
[]
]
}
还有这个
{
$unwind: {
path: '$users_sponsor',
preserveNullAndEmptyArrays: true
}
},
$cond: [
{ $gt: ['$users_car',0]},
{
_id: '$users_car._id',
make: '$users_car.make',
model: '$users_car.model',
year: '$users_car.year',
color: '$users_car.color',
},
[]
]
请提出建议