I have a document that contains companies that have offices all over the world. These offices are in an array. One of the values is the city and I need to know exactly which companies have 2 offices in "San Francisco".
The Document:
_id:52cdef7c4bab8bd675297d8b
name:"AdventNet"
.
.
.
.
Offices:Array
0:
Description:"Headquarters"
.
.
city:"San Francisco"
My Solution:
db.companies.aggregate(
{$group:{_id:{city:"$offices.city"},count:{$sum:1}}},
{$match:{$and: [{"_id.city":"San Francisco"},{count:2}]}})
But it doesn't work. Any Idea?