假设它们是用户模式和产品模式,如下所示:
userSchema: {
name:{type:string},
myProducts:[
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product',
}
]
}
productSchema: {
name: {type:string},
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}
用户架构具有从产品架构引用的产品数组,而产品架构具有引用到用户的所有者。
那么,要获取特定用户的所有产品,下面哪个查询更好、更优。
- const myProducts =等待用户查找({id:当前用户}).populate('我的产品');
- const myProducts =等待产品.查找({所有者:当前用户});
如果我们必须执行几个功能,如分页,搜索模式等,哪一个更好。
1条答案
按热度按时间eqfvzcg81#