Last active
August 29, 2015 13:57
-
-
Save A/9706432 to your computer and use it in GitHub Desktop.
This file contains 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
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