Using Mongoose with MongoDB, I need to return a series of lookups based on a field of an initial lookup. My Schema is as follows:
var PartSchema = new Schema({
partcode: String,
children: [String]
});
And the data looks like the following:
[{"partcode":"A1","children":["B1","B2","B3","B4"]},
{"partcode":"B1","children":["C11","C21","C31","C41"]},
{"partcode":"B2","children":["C12","C22","C32","C42"]},
{"partcode":"B3","children":["C13","C23","C33","C43"]},
{"partcode":"B4","children":["C14","C24","C34","C44"]}]
I can query for A1's children field by using the following static call:
var childrenOnly =
{children: 1, _id: 0};
PartSchema.static('getChildren', function (partcode, callback) {
return this.find({ partcode: partcode }, childrenOnly, callback);
});
This returns (via express)
[{"children":["B1","B2","B3","B4"]}]
What I need it to return is
[{"partcode":"B1","children":["C11","C21","C31","C41"]},
{"partcode":"B2","children":["C12","C22","C32","C42"]},
{"partcode":"B3","children":["C13","C23","C33","C43"]},
{"partcode":"B4","children":["C14","C24","C34","C44"]}]
I'm guessing an iterative call is required with the first query returning the children array, and then iterating over the children array to get each of the child records.