如果我有 2 个收藏:
产品
和类别
产品有类别引用...
如何按类别查询商品?
我需要这样的东西:
Product.find({category.name: "testCat"}, function() {
...
})
有什么解决办法吗?
最佳答案
看看$lookup ,您可以在其中加入
两个集合
db.product.aggregate(
{
$lookup: {
from: 'category',
localField: 'categoryRef',
foreignField: 'catId',
as: 'category'
},
{$unwind: '$category'},
{$match: {'category.name': "testCat"}}
})
关于javascript - 如何用另一个集合查询主集合[Mongoose],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43190915/