I have 3 collections as follows:
GroupRoles Collection:
//1
{
"_id": ObjectId("62a384ee0c4dbafc64000fba"),
"name": "GroupRole template 1",
"groupRoles": [
{
"_id": ObjectId("6298503f8a31000024002107"),
"members": [
ObjectId("629e1bb117366c39bc7d78e1")
]
},
{
"_id": ObjectId("629850368a31000024002106"),
"members": [
ObjectId("629ee6d502877d0f93f5dabe"),
ObjectId("629ee6d002877d0f93f5dab8")
]
},
{
"_id": ObjectId("6298502f8a31000024002105"),
"members": [ ]
},
{
"_id": ObjectId("629850288a31000024002104"),
"members": [ ]
},
{
"_id": ObjectId("6298501f8a31000024002103"),
"members": [ ]
},
{
"_id": ObjectId("629850128a31000024002102"),
"members": [ ]
}
]
}
Role Collections:
// 1 {"_id": ObjectId("629850128a31000024002102"), "type": 1, "name": "Role 1",}
// 2 {"_id": ObjectId("6298501f8a31000024002103"), "type": 1, "name": "Role 2",}
// 3 {"_id": ObjectId("629850288a31000024002104"), "type": 1, "name": "Role 3",}
// 4 {"_id": ObjectId("6298502f8a31000024002105"), "type": 1, "name": "Role 4",}
// 5 {"_id": ObjectId("629850368a31000024002106"), "type": 1, "name": "Role 5",}
// 6 {"_id": ObjectId("6298503f8a31000024002107"), "type": 1, "name": "Role 6",}
and User Collection:
// 1 {"_id": ObjectId("629e1bb117366c39bc7d78e1"), "email": "abc1#gmail.com", "name": "user 01"}
// 2 {"_id": ObjectId("629ee6d502877d0f93f5dabe"), "email": "abc2#gmail.com", "name": "user 02"}
// 3 {"_id": ObjectId("629ee6d002877d0f93f5dab8"), "email": "abc3#gmail.com", "name": "user 03"}
How can select GroupRoles with output format like this:
{
"_id": ObjectId("62a384ee0c4dbafc64000fba"),
"name": "GroupRole template 1",
"groupRoles": [
{
"_id": ObjectId("6298503f8a31000024002107"),
"type": 1,
"name": "Role 6",
"members": [
{
"_id": ObjectId("629e1bb117366c39bc7d78e1"),
"email": "abc1#gmail.com",
"name": "user 01"
}
]
},
{
"_id": ObjectId("629850368a31000024002106"),
"type": 1,
"name": "Role 5",
"members": [
{
"_id": ObjectId("629ee6d502877d0f93f5dabe"),
"email": "abc2#gmail.com",
"name": "user 02"
},
{
"_id": ObjectId("629ee6d002877d0f93f5dab8"),
"email": "abc3#gmail.com",
"name": "user 03"
}
]
},
{
"_id": ObjectId("6298502f8a31000024002105"),
"type": 1,
"name": "Role 4",
"members": [ ]
},
{
"_id": ObjectId("629850288a31000024002104"),
"type": 1,
"name": "Role 3",
"members": [ ]
},
{
"_id": ObjectId("6298501f8a31000024002103"),
"type": 1,
"name": "Role 2",
"members": [ ]
},
{
"_id": ObjectId("629850128a31000024002102"),
"type": 1,
"name": "Role 1",
"members": [ ]
}
]
}
I've tried aggregation and lookup, but it didn't get the results I wanted.
here is my aggregation query
The list of members is not in the correct index of groupRoles
[
{
'$lookup': {
'from': 'roles',
'localField': 'groupRoles._id',
'foreignField': '_id',
'as': 'temp_roles'
}
}, {
'$lookup': {
'from': 'users',
'localField': 'groupRoles.members',
'foreignField': '_id',
'as': 'temp_users'
}
}, {
'$addFields': {
'groupRoles': {
'$map': {
'input': '$groupRoles.members',
'as': 'mems',
'in': {
'members': {
'$filter': {
'input': '$temp_users',
'as': 'temu',
'cond': {
'$in': [
'$$temu._id', '$$mems'
]
}
}
}
}
}
}
}
}, {
'$addFields': {
'groupRoles': {
'$map': {
'input': {
'$zip': {
'inputs': [
'$groupRoles', '$temp_roles'
]
}
},
'in': {
'$mergeObjects': '$$this'
}
}
}
}
}, {
'$unset': [
'temp_roles', 'temp_users'
]
}, {
'$facet': {
'metadata': [
{
'$group': {
'_id': null,
'total': {
'$sum': 1
}
}
}
],
'data': []
}
}, {
'$project': {
'data': {
'_id': 1,
'name': 1,
'groupRoles': {
'_id': 1,
'type': 1,
'name': 1,
'members': {
'_id': 1,
'name': 1,
'email': 1
}
}
},
'total': {
'$arrayElemAt': [
'$metadata.total', 0
]
}
}
}
]
please help me find the solution.
Revise your query and thanks for the hints.
The issue is on $zip, which I think this operator is not suitable. You can check the behavior of $zip.
Combine both $addFields stages into one.
Iterate each value in groupRoles and with $mergeObjects for
Current iterate value.
The first match document from the temp_roles array by matching _id.
The document with members array.
db.groupRoles.aggregate([
{
"$lookup": {
"from": "roles",
"localField": "groupRoles._id",
"foreignField": "_id",
"as": "temp_roles"
}
},
{
"$lookup": {
"from": "users",
"localField": "groupRoles.members",
"foreignField": "_id",
"as": "temp_users"
}
},
{
"$addFields": {
"groupRoles": {
"$map": {
"input": "$groupRoles",
"as": "gr",
"in": {
"$mergeObjects": [
"$$gr",
{
$first: {
"$filter": {
"input": "$temp_roles",
"as": "r",
"cond": {
$eq: [
"$$gr._id",
"$$r._id"
]
}
}
}
},
{
"members": {
"$filter": {
"input": "$temp_users",
"as": "mem",
"cond": {
"$in": [
"$$mem._id",
"$$gr.members"
]
}
}
}
}
]
}
}
}
}
},
{
"$unset": [
"temp_roles",
"temp_users"
]
},
])
Sample Mongo Playground
Related
return the order with the user where purchase id: 123, product id: p123 and user and order shipping.mode =2
db={
"orders": [
{
"_id": ObjectId("62155381877d4300196008ef"),
"shipping": {
"mode": 1
},
"products": [],
"user": ObjectId("6186bd3315a342001bd84f42"),
},
{
"_id": ObjectId("6215569b54cc7f0030c44e0f"),
"shipping": {
"mode": 2
},
"user": ObjectId("6186bd3315a342001bd84f43"),
"products": [
{
"id": "p123"
}
],
}
],
"users": [
{
"_id": ObjectId("6186bd3315a342001bd84f43"),
"shipping": {
"mode": 2
},
"name": "user100",
"purchase": [
{
"id": "123"
},
{
"id": "hjhh"
}
],
}
]
}
https://mongoplayground.net/p/IomR8U7Ard-
db.orders.aggregate([
{
"$lookup": {
"from": "users",
"localField": "user",
"foreignField": "_id",
"as": "user"
}
},
{
"$unwind": "$user"
},
{
"$unwind": "$user.purchase"
},
{
"$match": {
"$and": [
{
"shipping.mode": {
"$gt": 0
}
},
]
}
},
{
"$match": {
"$or": [
{
"$and": [
{
"user.shipping.mode": {
"$eq": 2
}
},
{
"user.purchase.id": {
"$eq": "123"
}
},
{
"$expr": {
"$in": [
"p123",
"$products.id"
]
}
}
]
},
]
}
},
])
I have two collections, 1. temporaryCollection, 2. permanentCollection, I would like to take data from temporaryCollection and update in permanentCollection. To see the expected result see updatedPermanentCollection below.
Fields that are taken from Temporary collection and updated in Permanent collection are:
emailAddresses
phoneNumbers
ContactName
ContactNumber
For your info, the fields that are changed in Temporary collection
contacts[0]['emailAddresses']
contacts[0]['ContactName']
contacts[0]["phoneNumbers"]
contacts[0]["ContactNumber"]
Field that are that should not be changed after updation in UpdatedPermanentCollection is
contacts._id
Note: contacts is an Array of objects, for simplicity I have shown just one object.
I am currently using the below query which updates the permanentCollection but also overrides the contacts._id field. I don't want the contacts._id field to be overridden.
Here is my MongoDB Query
db.temporaryCollection.aggregate([
{
$match: {
userID: ObjectId("61d1efea2c0fab00340f47c8"),
},
},
{
$merge: {
into: "permanentCollection",
on: "userID",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
]);
1. temporaryCollection
{
"_id": { "$oid": "61d1f04266289f003452d705" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [
{ "id": "6884", "label": "email1", "email": "addedemail#gmail.com" }
],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292930"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d744" },
"ContactName": "Sample User 1 Name Changed",
"ContactNumber": "+918984292930",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
2. permanentCollection
{
"_id": { "$oid": "61d1f04266289f003452d701" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292929"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d722" },
"ContactName": "Sample User 1",
"ContactNumber": "+918984292929",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
3. updatedPermanentCollection (Expected result)
{
"_id": { "$oid": "61d1f04266289f003452d701" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [
{ "id": "6884", "label": "email1", "email": "addedemail#gmail.com" }
],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292930"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d722" },
"ContactName": "Sample User 1 Name Changed",
"ContactNumber": "+918984292930",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
Try with this aggregation query.
db.temporarCollection.aggreagate(
[
{
"$lookup": {
"from": "permanantCollection",
"let": {
"user_id": "$userID"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$$user_id", "$userID"
]
}
}
}
],
"as": "pcontacts"
}
}, {
"$unwind": {
"path": "$pcontacts",
"preserveNullAndEmptyArrays": true
}
}, {
"$project": {
"contacts": {
"$map": {
"input": "$contacts",
"as": "contact",
"in": {
"tcontact": "$$contact",
"pcontact": {
"$first": {
"$filter": {
"input": "$pcontacts.contacts",
"as": "pcontact",
"cond": {
"$eq": [
"$$pcontact.recordID", "$$contact.recordID"
]
}
}
}
}
}
}
},
"userNumber": 1,
"userID": 1,
"_id": 0
}
}, {
"$project": {
"contacts": {
"$map": {
"input": "$contacts",
"as": "contact",
"in": {
"emailAddresses": "$$contact.tcontact.emailAddresses",
"phoneNumbers": "$$contact.tcontact.phoneNumbers",
"ContactName": "$$contact.tcontact.ContactName",
"ContactNumber": "$$contact.tcontact.ContactNumber",
"recordID": {
"$let": {
"vars": {},
"in": {
"$cond": {
"if": "$$contact.pcontact.recordID",
"then": "$$contact.pcontact.recordID",
"else": "$$contact.tcontact.recordID"
}
}
}
},
"_id": {
"$let": {
"vars": {},
"in": {
"$cond": {
"if": "$$contact.pcontact._id",
"then": "$$contact.pcontact._id",
"else": "$$contact.tcontact._id"
}
}
}
}
}
}
},
"userNumber": 1,
"userID": 1
}
}, {
"$merge": {
"into": "pc",
"on": "userID",
"whenMatched": "replace",
"whenNotMatched": "insert"
}
}
])
It is not a fully optimized query but it works.
Try to add $unset to db query.
db.temporaryCollection.aggregate([
{
$unset: "_id"
},
{
$match: {
userID: ObjectId("61d1efea2c0fab00340f47c8"),
},
},
{
$merge: {
into: "permanentCollection",
on: "userID",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
]);
I have the following collections which I am using a $lookup to bring together:
{
"organizations": [
{
"_id": 1,
"name": "foo",
"users": [1,2]
},
{
"_id": 2,
"name": "bar",
"users": [1]
}
],
"users": [
{
"_id": 1,
"name": "john1 smith"
},
{
"_id": 2,
"name": "bob johnson"
}
]
}
The query works fine:
[{
"$lookup": {
"from": "users",
"localField": "users",
"foreignField": "_id",
"as": "members"
}
},
{
"$unwind": "$members"
},
{
"$group": {
"_id": "$_id",
"original": { "$first": "$$ROOT" },
"members": {
"$push": "$members"
}
}
}
]
however, the resulting organizations records don't return all of their properties without adding the original prop which gives me back a nesting of the original organization:
{
"_id": 1,
"original": {
"_id": 1,
"name": "foo",
"users": [
1,
2
],
"members": {
"_id": 1,
"name": "john1 smith"
}
},
"members": [
{
"_id": 1,
"name": "john1 smith"
},
{
"_id": 2,
"name": "bob johnson"
}
]
}
I'm trying to get everything in the original prop back into the root along with the new members array.
I am struggling with writing an aggregation pipeline to lookup nested documents by their _id and return a specific name without overwriting the existing keys/values in the data. I have managed to do this for the nested array, but am unable to do it for an array that is nested within the nested array.
So I want to lookup the _id of each ingredient and each subIngredient and merge them with the data for these ingredients that already exists (i.e. qty, measure).
Here is what I have so far:
https://mongoplayground.net/p/ft4oIMm_8wg
Products Collection:
[
{
"_id": {
"$oid": "5ecf269bceb735416db0b329"
},
"id": 36,
"title": "Product 1",
"description": {
"generalInformation": "Some information",
"activeIngredients": [
{
"_id": 1636,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1626,
"qty": 16.6,
"measure": "µg"
}
],
},
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
},
{
"_id": 1212,
"qty": 16.6,
"measure": "µg"
}
],
},
]
},
},
{
"_id": {
"$oid": "5ecf269bceb735416db0b346"
},
"id": 36,
"title": "Product 2",
"description": {
"generalInformation": "Some information",
"activeIngredients": [
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
}
],
},
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
},
{
"_id": 1212,
"qty": 16.6,
"measure": "µg"
}
],
},
]
},
}
]
Ingredients Collection:
[
{
"_id": 1234,
"name": "Ingredient 1",
},
{
"_id": 1122,
"name": "Ingredient 2",
},
{
"_id": 1212,
"name": "Ingredient 3",
},
{
"_id": 1636,
"name": "Ingredient 4",
},
{
"_id": 1626,
"name": "Ingredient 5",
}
]
What should be returned:
[
{
"_id": ObjectId("5ecf269bceb735416db0b329"),
"title": "Product 1"
"description": {
"activeIngredients": {
"_id": 1636,
"measure": "µg",
"name": "Ingredient 4",
"qty": 133.5,
"subIngredient": [
{
"_id": 1626,
"measure": "µg",
"qty": 16.6
}
]
},
"generalInformation": "Some information"
},
"ingredients": [
{
"_id": 1636,
"measure": "µg",
"name": "Ingredient 4",
"qty": 133.5,
"subIngredient": [
{
"_id": 1626,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
}
]
},
{
"_id": 1234,
"measure": "µg",
"name": "Ingredient 1",
"qty": 133.5,
"subIngredient": [
{
"_id": 1122,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
},
{
"_id": 1212,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
}
]
}
]
},
]
My current pipeline:
[
{
"$unwind": {
"path": "$description.activeIngredients",
"preserveNullAndEmptyArrays": false
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "description.activeIngredients._id",
"foreignField": "_id",
"as": "description.activeIngredients.name"
}
},
{
"$addFields": {
"description.activeIngredients.name": {
"$arrayElemAt": [
"$description.activeIngredients.name.name",
0
]
}
}
},
{
"$group": {
"_id": "$_id",
"ingredients": {
"$push": "$description.activeIngredients"
},
"description": {
"$first": "$description"
},
"title": {
"$first": "$title"
}
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "ingredients.subIngredient._id",
"foreignField": "_id",
"as": "subIngredients"
}
}
]
Appreciate any help. Thanks.
You're not far off and you can achieve this result in multiple different ways, one of which is to just $unwind the subingredients array, $lookup on that and finally adding another $group stage to restructure the document.
As you've clearly shown you know how to do all these things i'll show a different way that utilizes operators like $map, $indexOfArray and Mongo's v3.6 $lookup syntax.
The strategy is instead of unwinding the subarray we just $lookup all the relevant sub-ingredients and then "merge" the two arrays using the operators i specified.
i.e taking:
[ {id: 5, name: "name"} ];
[ {id: 5, qty: 25} ]
And making them into:
[ {id: 5, name: "name", qty: 25} ]
Here's how we do it:
db.products.aggregate([
{
"$unwind": {
"path": "$description.activeIngredients",
"preserveNullAndEmptyArrays": false
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "description.activeIngredients._id",
"foreignField": "_id",
"as": "description.activeIngredients.name"
}
},
{
"$addFields": {
"description.activeIngredients.name": {
"$arrayElemAt": [
"$description.activeIngredients.name.name",
0
]
}
}
},
{
"$lookup": {
"from": "ingredients",
"let": {
sub: "$description.activeIngredients.subIngredient"
},
"pipeline": [
{
$match: {
$expr: {
"$setIsSubset": [
[
"$_id"
],
{
$map: {
input: "$$sub",
as: "datum",
in: "$$datum._id"
}
}
]
}
}
}
],
"as": "subIngredients"
}
},
{
"$addFields": {
"description.activeIngredients.subIngredient": {
$map: {
input: "$description.activeIngredients.subIngredient",
as: "sub",
in: {
"$mergeObjects": [
"$$sub",
{
name: {
$arrayElemAt: [
"$subIngredients.name",
{
"$indexOfArray": [
"$subIngredients._id",
"$$sub._id"
]
}
]
}
}
]
}
}
}
}
},
{
"$group": {
"_id": "$_id",
"ingredients": {
"$push": "$description.activeIngredients"
},
"description": {
"$first": "$description"
},
"title": {
"$first": "$title"
}
}
}
])
MongoPlayground
How to populate in result of aggregated query in monogdb
Array of followedId
var followeduserId = ["abc","efg","xyz","pqr","acd","rts"];
Feeds Recommended
[
{
"feedsId": "feed1",
"userId": "abc"
},
{
"feedsId": "feed1",
"userId": "efg"
}
]
Feeds collection
[
{
"link": "www.yodo.com",
"recommended": [
"abc",
"efg"
],
"title": "This is my feed7",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
},
{
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"das",
"asd",
"eqw",
"weq"
],
"title": "This is my feed8",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
}
]
Ran aggregation query
feedsrecommended.aggregate([
{ $match: { userId: { $in: "followersId" }}},
{ $lookup: {
from: "feeds",
localField: "feedsId",
foreignField: "_id",
as: "feedsId"
}},
{ $group: {
"_id": { "feedsId": "$feedsId" },
"count": { "$sum": 1 }
}},
{ $sort: { count: -1 }}
])
result After aggregation
var resultfeeds = [
{
"count": 7,
"id": {
"_id": "feed1",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"xyz",
"pqr",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi8",
"topi6",
"topi5"
]
}
},
{
"count": 3,
"id": {
"_id": "feed5",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
}
},
{
"count": 3,
"id": {
"_id": "feed6",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"xyz",
"pqr"
],
"title": "This is my feed1",
"topics": [
"topi7",
"topi1",
"topi4",
"topi8"
]
}
},
{
"count": 2,
"id": {
"_id": "feed2",
"link": "www.yodo.com",
"recommended": [
"abc",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi7",
"topi6",
"topi8"
]
}
},
{
"count": 2,
"id": {
"_id": "feed7",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi5",
"topi6",
"topi4"
]
}
},
{
"count": 1,
"id": {
"_id": "feed3",
"link": "www.yodo.com",
"recommended": [
"abc",
"asd",
"eqw",
"weq"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi7",
"topi6",
"topi4"
]
}
},
{
"count": 1,
"id": {
"_id": "feed8",
"link": "www.yodo.com",
"recommended": [
"abc",
"das",
"asd",
"eqw",
"weq"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi2",
"topi5",
"topi4"
]
}
}
]
I want to populate topics and recommeded userName and image in the result
topic collection
[
{
"topic_name": "tiger"
},
{
"topic_name": "loin"
}
]
user collection
[
{
"name": "deepa",
"profileImg": "www.com/facebook.jpg"
},
{
"name": "nisa",
"profileImg": "www.com/facebook.jpg"
}
]
My last result should be like this
[
{
"count": 2,
"id": {
"_id": "feed2",
"link": "www.yodo.com",
"recommended": [
{
"_id": "abc",
"name": "deepa",
"profileImg": "www.com/facebook.jpg"
},
{
"_id": "acd",
"name": "sigger",
"profileImg": "www.com/facebook.jpg"
},
{
"_id": "rts",
"name": "buster",
"profileImg": "www.com/facebook.jpg"
}
],
"title": "This is my feed1",
"topics": [
{
"_id": "topi6",
"topic_name": "boolena"
},
{
"_id": "topi7",
"topic_name": "mika"
},
{
"_id": "topi8",
"topic_name": "tika"
}
]
}
}
]
You can try below aggregation in mongodb 3.6 and above
Feedsrecommended.aggregate([
{ "$match": { "userId":{ "$in": followersId }}},
{ "$group": {
"_id": "$feedsId",
"count": { "$sum": 1 }
}},
{ "$lookup": {
"from": "feeds",
"let": { "feedsId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$feedsId" ] }}},
{ "$lookup": {
"from": "topics",
"let": { "topics": "$topics" },
"pipeline": [
{ "$match": { "$expr": { "$in": [ "$_id", "$$topics" ] } } }
],
"as": "topics"
}},
{ "$lookup": {
"from": "users",
"let": { "recommended": "$recommended" },
"pipeline": [
{ "$match": { "$expr": { "$in": [ "$_id", "$$recommended" ] } } }
],
"as": "recommended"
}}
],
"as": "feedsId"
}}
])