Node.js Mongoose中的嵌套类别查询
您好,如果可能的话,我需要通过一个查询进行此操作。 在我的产品模式中,我定义了与类别的多对多关系,所以我有类似的东西Node.js Mongoose中的嵌套类别查询,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,您好,如果可能的话,我需要通过一个查询进行此操作。 在我的产品模式中,我定义了与类别的多对多关系,所以我有类似的东西 categories: [{type: Schema.Types.ObjectId, ref: 'Category'}] 类别可以有子类别,所以我在类别模式中有这个 parent: {type: Schema.Types.ObjectId, ref: 'Category', default:null} 我想进行查询,以获取类别内的产品及其下的所有子类别,并且我只有父类
categories: [{type: Schema.Types.ObjectId, ref: 'Category'}]
类别可以有子类别,所以我在类别模式中有这个
parent: {type: Schema.Types.ObjectId, ref: 'Category', default:null}
我想进行查询,以获取类别内的产品及其下的所有子类别,并且我只有父类别id
这是我现在所拥有的,但我希望有一个查询
var findProductsByCategories = function (ids) {
var promise = Product.find({categories: {$in: ids}}).exec();
promise.then(function (products) {
res.send(products);
});
}
// Get this category children
var promise = Category.find({parent: req.params.category}).select('_id').exec();
promise.then(function (children_ids) {
// Add the parent id as well
children_ids.push(req.params.category);
return children_ids;
}).then(findProductsByCategories);