【发布时间】:2016-01-13 05:30:36
【问题描述】:
给定以下 mongodb 文档:
db.customers
{
"name" : "customer 1",
"merchants" : [
{ "name" : "test merchant 1" },
{ "name" : "test merchant 2" },
{ "name" : "test merchant 3" }
]
}
{
"name": "customer 2",
"merchants" : [
{ "name" : "test merchant 1" }
]
}
我将如何查找并仅退回拥有多个商家的客户。
来自 SQL 背景,相当于:
Customers Table:
id int(11),
name char(56)
Merchants Table:
name char(56),
customer_id int(11)
select customer.id, count(merchants.id) as m_count
from
customers, merchants
where
customers.id = merchants.customer_id
group by
customers.id
having
m_count > 1;
我将如何在 mongodb 中完成此操作?我已经使用聚合来获取商家的数量,但不知道如何根据数量过滤结果。也许在 mongodb 中有一种完全不同的方式来处理它......
【问题讨论】:
标签: mongodb