我想知道如何才能对数据库进行查询,得到满足‘store’{name}字段的所有结果,但这个‘store’是用‘_id’保存的,我使用.opolate()为我带来信息
/**示例:aWait Product.find({store:{name:"特定store"}})*/
const products = await Product.find({ 'store.name' : "Ejemplo1" })
.populate('store', 'name')
有关计划如下:
const storeSchema = mongoose.Schema({
_id:"it is added automatically by mongoose",
name: String
})
export default model('Store', storeSchema);
const productSchema = mongoose.Schema({
store: {
type: Schema.Types.ObjectId,
ref: "Store"
}
})
基本上我想要实现的是从数据库中提取所有产品与特定的‘store ’,但我仍然不能做到这一点,我感谢任何帮助解决这个问题,无论是一个参考或一个合适的查询的例子,提前感谢
我试过这个: Const p=等待产品.查找({}) .填充({ 路径:‘store’, 匹配:{名称:{$eq:‘Store1’}}, Select :‘name-_id’, })
但它返回整个集合,并且不进行过滤,我收到如下内容: { _id:1, ... 存储:空, ... } { _id:2, ... store :{名称:"store 1"}, ... } { _id:3, ... store :{名称:"store 2"}, ... }