I'm using mongodb v.2.6.11 and mongoose.
I have two models with a OneToMany relationship. In my parent, TypeA, (Abbreviated TA) I have no references to the child. In my child, TypeB (abbreviated TB) I have an id reference to the parent.
Example Schema:
var TASchema = {
name: {type: String, required: "TASchema.name is required", index: {unique: true, dropDups: true}}
};
var TBSchema = {
start: {type: Number, required: "TBSchema.language is required"},
stop: {type: Number, required: "TBSchema.stop is required"},
TA: {type: Schema.Types.ObjectId, ref: 'tbschema'},
}
What I want to do: Select all TB that have the "start" variable within a timespan of the two variables "ts_start" and "ts_stop" (they're timestamps). So something like: start : {$gte: ts_min, $lte : ts_max}
.
Example output:
[
{
name: "Ta object 1",
tbs: [{start : 1, stop2}, {start: 2, stop: 3}]
},
{
name: "Ta object 2",
tbs: [{start : 1, stop2}, {start: 2, stop: 3}]
}
]
I want to keep the structure where the query returns an array of TA where every TA contains an array of TB:s. But I cannot use populate
since the TA doesn't have any reference to the child (because there can be too many to keep them as sub documents).
So how is this achievable? Am I thinking wrong or what should I do to output the specified query as in my example output?