Skip to content

Instantly share code, notes, and snippets.

@A
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A/9706432 to your computer and use it in GitHub Desktop.
Save A/9706432 to your computer and use it in GitHub Desktop.
module.exports = function () {
var ObjectId = mongoose.Types.ObjectId;
return function (req, res) {
var type = capitalize(req.params.componentType);
var id = new ObjectId(req.params.componentID);
var matchQuery = {};
matchQuery['pages.' + map[type] + '._id'] = id;
Project.aggregate([
// Match documents
{ '$match': matchQuery },
// Unwind the pages array
{ '$unwind': '$pages' },
// Unwind the 'texts' array
{ '$unwind': '$pages.' + [map[type]] },
// Filter array
{ '$match': matchQuery},
// Re-form
{ '$group': {
'_id': '$_id',
'data': { '$push': '$pages.' + [map[type]]}
}}
],
function(err, projects) {
err
? res.json(500, {error: err.message})
: res.json(200, {data: projects[0].data[0]});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment