我想加入一个集合,然后在 mongodb 中找到。
这是我的收藏:
颜色:
{id: 1, color: 'red'}
{id: 2, color: 'blue'}
价格:
{price_id: 1, price: 4}
{price_id: 2, price: 5}
如何找到价格为 5 美元的颜色?
这是我的解决方案(不起作用):
db.collection('colors').aggregate([
{
$lookup:
{
from: 'prices',
localField: 'id',
foreignField: 'price_id',
as: 'info'
},
$match:
{
price: 5
}
}
]).toArray((res, err) => {
if (res) {
console.log(res);
} else {
console.log(err);
}
})