I'm having a problem using $lookup in my aggregation pipeline.
I have 2 collections, members & messages
members :
{_id, FirstName, LastName, Email, ...}
messages
{
_id:ObjectId('xxx'),
createdBy:ObjectId(''),
...
threads:[
{ message:'' , attachments:[] , from:ObjectId , to:[{status:'Read' , recipient:ObjectId}] }]
}
What I'm trying to do is,
lookup for each recipient in : to:[{status:'Read' , recipient:ObjectId}] and populate name and email from members collection.
I tried many different things like this one; //
db.messages.aggregate([
{
'$lookup': {
'from': 'members',
'let': {
'memberId': '$threads.to.recipient'
},
'pipeline': [
{
'$match': {
'$expr': {
'$eq': [
'$$memberId', '$members._id'
]
}
}
},
{$project: {FirstName: 1, _id: 1, LastName: 1, Email: 1}}
],
'as': 'members'
}
}
]
Many different queries including this one always return [] for members ('as': 'members').
Just to test I tired with mongoose and .populate('threads.to.recipient','FirstName') worked perfectly. But I cannot use mongoose for this I have to use MongoDB's native nodejs driver.
any advice would be greatly appreciated on this...