I have the following model in mongo db:
User collection
{
_id:12345,
name:"Joe",
age:15,
}
Addresses collection
{
_id:7663,
userId:12345,
Street:"xyz",
number:"1235",
city:"New York",
state:"NY"
}
Now I want to get all the addresses of users above the age of 20. What I thought was to query all the ids of users above 20 and with the result of this query use the $in operator to find the addresses.
My question is, is there a way to turn this into one query? Is there a better way to query this? (obs: this is just an example, with my problem I cannot embed addresses into users)
$in
to do this and you have a specific question about a problem with the approach, then that works better. – Veneerdb.users.find({age:{$gt:20}})
And I know I can query the list of addresses like this:db.addresses.find({userId:{$in:[123,124,125,126]}})
I know I can project the first query to return only the Ids, the only thing I dont know is if I can use the result of the first query in the second one. If this is not possible, is there any other way I can query the second collection with a result from the first? – Scriven