Skip to content

Instantly share code, notes, and snippets.

@aaBoustani
Created September 9, 2017 13:58
Show Gist options
  • Select an option

  • Save aaBoustani/08bf46403ca164f4bb6659532550402c to your computer and use it in GitHub Desktop.

Select an option

Save aaBoustani/08bf46403ca164f4bb6659532550402c to your computer and use it in GitHub Desktop.
use native function to query mongodb
module.exports = {
find (req, res) {
Model.native((error, collection) => {
if (error) return res.badRequest(error);
return collection.aggregate([{
$match: {
id: { $in: req.ids }
}
},
{
$project: {
title: 1,
weight: {
$cond: [{ $eq: ['$title', req.query] }, 3, 2]
}
}
},
{ $sort: { weight: -1 } }
])
.toArray((err, result) => err ? res.badRequest(err) : res.ok(result));
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment