I have a function in one of my controllers where I populate an array of references to a document, which, when populated, have embedded arrays themselves.
Here's an example:
The mongoose populate function gives me an array of objects. Within each of those objects is an array:
[{ name: Test, array: [ 1, 2, 3, 4, 5 ] }, { name: TestAgain, array: [1, 2, 3, 4, 5] }, { name: Test^3, array: [1, 2, 3, 4, 5]}, {...
The desired output would be:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...]
I need to concatenate all of the "arrays" within the populated references. How can I do this without knowing how many arrays there are?
For reference, here is (generally) what my function looks like:
exports.myFunctionName = function ( req, res, next )
Document.findOne({ 'document_id' : decodeURIComponent( document_id )}).populate('array_containing_references').exec(function( err, document)
{
//Here I need to concatenate all of the embedded arrays, then sort and return the result as JSON (not worried about the sorting).
});