I have the 3 collections
users: [
{_id: "aaa", name: "John", department: 1},
{_id: "bbb", name: "Charles", department: 1},
{_id: "ccc", name: "Jessy", department: 1"},
{_id: "ddd", name: "Tim", department: 2},
{_id: "eee", name: "Max", department: 2},
{_id: "fff", name: "Julia", department: 2},
{_id: "ggg", name: "Arnold", department: 3}
]
departments: [
{_id: 1, name: "press", society: "times"},
{_id: 2, name: "news", society: "times"},
{_id: 3, name: "infos", society: "herald"}
]
society: [
{name: "times", country: "England"},
{name: "herald", country: "USA"}
]
A user work in a department and a department is in a society.
I wanto to do 2 requests, the first one is to have all users from the society "times" and the second, is to have all users from the country "England".
I tried this request for the first one :
db.users.aggregate([
{'$match': {'dept.society': "times"}
{
'$lookup': {
'from': "departments",
'localField': "department",
'foreignField': "_id",
'as': "dept"
}
}])
But because of the condition is on foreign collection ("departments"), it seems not working. Only condition on local collection works ('department': 1). How I can do that, and it's the same problem for country request?