Reputation: 11
Given:
var productSchema = new Schema({
name: String,
details : [{ name: String, description: String }]
})
var listSchema = new Schema({
name: String,
products: [{
product_id: { type: Schema.Types.ObjectId, ref: 'Product' },
product_vid: { type: Schema.Types.ObjectId, ref: 'Product.details' }
}]
})
How can I do the query for the List with only corresponding product_id with one details matching product_vid in it?
List.findById(list_id)
.populate({
path: 'products.product_id',
populate: {
path: 'products.product_vid'
}
})
.exec(function(err, doc){
......
}
Upvotes: 1
Views: 712
Reputation: 490
There is no need for
product_vid: {
type: Schema.Types.ObjectId, ref: 'Product.details'}
in listSchema.
var productSchema = new Schema({
name: String,
details : [{ name: String, description: String }]
})
var listSchema = new Schema({
name: String,
products: [{ type: Schema.Types.ObjectId, ref: 'Product' }])
List.findById(list_id)
.populate({
path: 'products',
match: { details: "your matching value"})
.exec(function(err, doc){
......
}
Upvotes: 3
Reputation: 2636
This is wrong
product_vid: { type: Schema.Types.ObjectId, ref: 'Product.details' }
detail is field of a model. and not model itself.
it should be something like..
var listSchema = new Schema({
name: String,
products: [{ type: Schema.Types.ObjectId, ref: 'Product' }],
})
and then
populate('products')
or probably
populate('products products.details')
Just try and let me know.
Upvotes: 0