I'm trying to add a field to a lookup result with the node.js MongoDB driver. A user has two kinds of feeds 1. His or her own feeds and 2. Shared feeds. So i need to get both and then combine them. This is my original query which works fine:
client.db('atlas').collection('users').aggregate([
{ $match: { user_id: userId } },
{
$lookup: {
from: 'feeds',
localField: "user_id",
foreignField: "user_id",
as: "feeds"
}
},
{
$lookup: {
from: 'shares',
localField: 'email',
foreignField: "share",
as: "shares"
}
},
{
$lookup: {
from: 'feeds',
localField: 'shares.feed_id',
foreignField: "_id",
as: "shared_feeds"
}
},
{
$addFields: { 'shared_feeds.share': true }
},
{
$project: {
user_id: 1,
first_name: 1,
last_name: 1,
email: 1,
feeds: { $concatArrays: [ '$feeds', '$shared_feeds' ] }
}
}
])
But I want to add a field to the second lookup on "feeds" so I tried using a pipeline like this:
{
$lookup: {
from: 'feeds',
let: { shareId: '$shares.feed_id' },
pipeline: [
{ $match: { '_id': ObjectId('$$shareId') } }
],
as: "shared_feeds"
}
}
But I can't get the match to work with ObjectId. The variable doesn't appear to be working. What am I doing wrong?
Sample collections: users:
[
{
"_id" : ObjectId("5e970a1f797c08e70ce4e2f6"),
"user_id" : "testuserid",
"first_name" : "Jacopo",
"last_name" : "Belbo",
"email" : "test@test.com"
}
]
feeds:
[
{
"_id" : ObjectId("5f29b02318ecbf0083f01f1e"),
"name" : "test csv",
"user_id" : "auth0|5eac6faa1cc1ac0c147b5b16",
"type" : "feed",
"created_at" : ISODate("2020-08-04T18:59:47.640Z"),
"updated_at" : ISODate("2020-08-04T18:59:47.640Z")
},
{
"_id" : ObjectId("5f29b05d18ecbf0083f01f1f"),
"name" : "typtap-small",
"user_id" : "auth0|5f1b2da0e60194003d714680",
"url" : "http://localstack:4572/atlas/usertes",
"type" : "feed",
"created_at" : ISODate("2020-08-04T19:00:45.886Z"),
"updated_at" : ISODate("2020-08-04T19:00:45.886Z")
}
]
shares:
[
{
"_id" : ObjectId("5f29b07318ecbf0083f01f20"),
"feed_id" : ObjectId("5f29b05d18ecbf0083f01f1f"),
"share" : "test@test.com",
"type" : "share",
"recipient" : "email",
"created_at" : ISODate("2020-08-04T19:01:07.668Z"),
"updated_at" : ISODate("2020-08-04T19:01:07.668Z")
}
]