var CarSchema = new Schema({
name: {type: String},
partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}],
});
var PartSchema = new Schema({
name: {type: String},
props: [
{ colour: {type: String}, shape: {type:String} }
],
});
For example
Car = {
name: "BMW",
partIds:[ObjectId("57baa43e152654f80aac36a6")]}
Part = {
_id: ObjectId("57baa43e152654f80aac36a6"),
name: "Piston",
props: [{colour:"red", shape: "Cubical"},{colour:"green", shape: "cylindrical"}]
So when I query I should get a doc like this:
Car = {
name: "BMW",
partIds: [{
_id:ObjectId("57baa43e152654f80aac36a6"), name:"Piston", props: [{colour:"red", shape:"cubical"}]
}
The props array should only have element with colour red
I want to populate Car with Part Array such that its prop array only have the object with colour red. Is there anyway to do it, or would I have to go old fashioned way and loop through props array matching its colour with red.
partCollection? Or do you create it and store its id incarCollection?partdocument and store its id incarcollection if the color is red?