Given the following mongodb documents:
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" }
]
}
How would I do a find and return only the customer with more than one merchant.
Coming from a SQL background, the equivalent would be:
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;
How would I accomplish this in mongodb? I've gotten as far as using an aggregate to get the count of merchants, but don't know how to filter the results based on the count. Maybe there's a whole different way to go about it in mongodb...