通过另一个数组中的另一个过滤$ project数组。
这就是我现在所拥有的。我尝试在另一个$ project上使用过滤器,但它返回一个空数组。我还尝试将约会数组映射到id数组,并使用$filter
$in: [ 'user._id', appointmentsIds ]
用户映射到
{
$match: {
provider_id: providerId,
},
},
{
$lookup: {
from: 'appointments',
localField: 'provider_id',
foreignField: 'provider_id',
as: 'appointments',
},
},
{
$project: {
_id: 1,
rate: 1,
provider_id: 1,
appointments: {
$filter: {
input: '$appointments',
as: 'appointment',
cond: {
$in: ['$$appointment.date', dates],
},
},
},
},
},
{
$lookup: {
from: 'fingerprints',
let: { id: '$_id' },
pipeline: [
{
$match: {
$expr: {
$in: ['$$id', '$visits.bussines_id'],
},
},
},
],
as: 'users',
},
},
{
$project: {
_id: 1,
rate: 1,
appointments: 1,
users: {
$filter: {
input: '$users',
as: 'user',
cond: {
$ne: ['$$user.user_id', { $toObjectId: '$provider_id' }],
},
},
},
},
},
{
$addFields: {
viewers: {
$map: {
input: '$users',
as: 'user',
in: {
visits: {
$filter: {
input: '$$user.visits',
as: 'visit',
cond: {
$in: ['$$visit.date', dates],
},
},
},
},
},
},
},
},
{
$project: {
_id: 0,
rate: 1,
viewers: { $size: '$viewers' },
appointmentsCount: { $size: '$appointments' },
users: 1,
appointments: 1,
},
},
我想保留最后一个$project
上的所有字段,并添加另一个字段,该字段的数组为users
,该数组由user_id
过滤,该数组位于appointments
数组中。