Skip to content

Instantly share code, notes, and snippets.

@hrishi7
Created July 13, 2020 12:06
Show Gist options
  • Save hrishi7/820b027ff00aaa142fe72a15a07ce5a6 to your computer and use it in GitHub Desktop.
Save hrishi7/820b027ff00aaa142fe72a15a07ce5a6 to your computer and use it in GitHub Desktop.
exports.getkeywordswithSkippingKeywords = async (req, res, next) => {
try {
const userId = req.userData.userId;
const limit = parseInt(req.params.limit);
const page = parseInt(req.params.page);
let keywords = await Keyword.aggregate([
{ $match: {} },
{ $sort: { count: -1 } },
{ $skip: limit * page },
{ $limit: limit },
{
$lookup: {
from: Preference.collection.name,
let: { keywordId: "$_id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$keyword", "$$keywordId"] },
{
$eq: ["$user", mongoose.Types.ObjectId(userId)],
},
],
},
},
},
],
as: "keywordData",
},
},
{
$project: {
_id: 0,
id: "$_id",
count: 1,
for: 1,
against: 1,
created_at: 1,
updated_at: 1,
keyword: 1,
selected: {
$cond: {
if: {
$eq: [{ $size: "$keywordData" }, 0],
},
then: false,
else: true,
},
},
},
},
]);
res.status(200).json({ success: true, data: keywords });
} catch (error) {
console.log(error);
res.status(500).json({ success: false, error });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment