需求描述:
mongodb中有两个collection,theme存储主题的主要信息,同时包含有产品id的数组,现在想通过产品的详细id找出products中产品的详细信息,应该如何查询呢?或者有更好的设计思想吗?
theme(主题)
{
'thmemId':'001',
'title':'theme1',
'productsId':[1,2,5]
}
products(产品)
{
'id':'1',
'title':'玩具',
'price':25
},
{
'id':'2',
'title':'鲜花',
'price':12
},
{
'id':'5',
'title':'电脑',
'price':4000
}
最终想实现的效果如下:
{
'thmemId':'001',
'title':'theme1',
'productsId':[
{
'id':'1',
'title':'玩具'
'price':25
},
{
'id':'2',
'title':'鲜花'
'price':12
},
{
'id':'5',
'title':'电脑'
'price':4000
}
]
}
populate
使用
populate
查询处理的数据会到Products
查找然后自动填充到productsId
里面