Мне нужен весь список таймингов, который соответствует tenantId и не удален в поле. Пример моей коллекции (корпоративной)
{
"tenantId":"Hello",
"timings":[
{
"type" : "delivery",
"pickAndPack" : NumberInt(3),
"category" : "premium",
"createdBy" : "nupur@yopmail.co",
"createdAt" : ISODate("2020-08-05T06:38:40.842+0000"),
"lastUpdatedAt" : ISODate("2020-08-05T06:54:22.641+0000"),
"deletedAt" : ISODate("2020-08-05T06:54:22.641+0000"),
"updatedBy" : null
},
{
"type" : "delivery",
"pickAndPack" : NumberInt(1),
"category" : "custom",
"createdBy" : "nupur@yopmail.co",
"createdAt" : ISODate("2020-08-05T06:38:24.857+0000"),
"lastUpdatedAt" : ISODate("2020-08-05T06:38:24.857+0000")
},
{
"type" : "delivery",
"pickAndPack" : NumberInt(1),
"category" : "premium",
"createdBy" : "nupur@yopmail.co",
"createdAt" : ISODate("2020-08-05T06:38:40.842+0000"),
"lastUpdatedAt" : ISODate("2020-08-05T06:38:40.842+0000")
}
]
}
Это то, что я пробовал ... но он не работает Результат запроса должен возвращать только время, которое не было удалено поле.
const corporate = await collection('corporate').aggregate([
// Get just the docs that contains timings matching the type
{ $match: {
tenantId ,
timings : {
$elemMatch : {
deletedAt: {
$exists: false
}
}
}
} },
{
$project: {
timings: {
$filter: {
input: '$timings',
as: 'timings',
cond: { $eq: ['$$timings.type', type] }
}
},
_id: 0
}
}
]).toArray();