Skip to content

Instantly share code, notes, and snippets.

@hrishi7
Created July 6, 2020 10:42
Show Gist options
  • Save hrishi7/b2d11cd0d927bfc7c569f90cc5a3aaa1 to your computer and use it in GitHub Desktop.
Save hrishi7/b2d11cd0d927bfc7c569f90cc5a3aaa1 to your computer and use it in GitHub Desktop.
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