Max
привет! Есть аггрегатный запрос, который хочется положить во вьюшку - проблема - когда выполняешь его, всё норм, но когда делаешь файнд по вьюшке - поле timeInCurrentStatus не подтягивется, как будто его и нету
[
'DriversListView',
{
viewOn: 'users',
pipeline: [
{ $match: { roles: { $in: ['driver'] } } },
{
$addFields: {
fullName: {
$concat: ['$firstName', ' ', '$lastName'],
},
},
},
{
$lookup: {
from: 'vehicles',
localField: 'driver.assignedVehicle',
foreignField: '_id',
as: 'assignedVehicle',
},
},
{
$lookup: {
from: 'countries',
localField: 'driver.address.country',
foreignField: 'name',
as: 'country',
},
},
{
$lookup: {
from: 'countries',
localField: 'driver.address.country',
foreignField: 'name',
as: 'country',
},
},
{
$lookup:
{
from: 'userstatushistories',
let: { driverId: '$_id' },
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{ $eq: ['$userId', '$$driverId'] },
{ $eq: ['$to', undefined] },
],
},
},
},
],
as: 'statushistories',
},
},
{
$unwind: {
path: '$statushistories',
preserveNullAndEmptyArrays: true,
},
},
{
$project: {
fullName: true,
searchId: true,
profileImage: true,
driverId: '$driver.id',
totalRating: '$driver.rating.value',
earningsTotal: '$driver.earnings.total',
phoneNumber: true,
status: '$driver.status',
missedRequestCount: '$driver.missedRequestCount',
rejectedRequestCount: '$driver.rejectedRequestCount',
location: '$driver.location',
licensePlateNumber: '$assignedVehicle.licensePlateNumber',
assignedVehicleId: '$assignedVehicle._id',
currency: '$country.currency',
timeInCurrentStatus: '$statushistories',
createdAt: true,
updatedAt: true,
},
},
{
$unwind: {
path: '$licensePlateNumber',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$assignedVehicleId',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$currency',
preserveNullAndEmptyArrays: true,
},
},
],
},
]