Created
July 6, 2020 10:42
-
-
Save hrishi7/b2d11cd0d927bfc7c569f90cc5a3aaa1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getPreferencesForLoggedinUser = async (req) => { | |
let userId = req.userData.userId; | |
let result = await Preference.aggregate([ | |
{ $match: { user: mongoose.Types.ObjectId(userId) } }, | |
{ | |
$lookup: { | |
from: Keyword.collection.name, | |
localField: "keyword", | |
foreignField: "_id", | |
as: "keywordData", | |
}, | |
}, | |
{ $unwind: "$keywordData" }, | |
{ | |
$project: { | |
_id: 0, | |
keyword: "$keywordData.keyword", | |
user: 1, | |
}, | |
}, | |
{ | |
$group: { | |
_id: "$user", | |
keywords: { | |
$push: "$keyword", | |
}, | |
}, | |
}, | |
{ | |
$project: { | |
_id: 0, | |
user: "$_id", | |
keywords: 1, | |
}, | |
}, | |
]); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment