How to write a JOIN query like this one in MongoDB form?
SELECT TABLE1.ID, TABLE2.MOBILE_PHONE
FROM TABLE2 INNER JOIN TABLE1 ON TABLE2.ID = TABLE1.ID
WHERE TABLE1.NUMBER IN ('1','2','3')
How to write a JOIN query like this one in MongoDB form?
SELECT TABLE1.ID, TABLE2.MOBILE_PHONE
FROM TABLE2 INNER JOIN TABLE1 ON TABLE2.ID = TABLE1.ID
WHERE TABLE1.NUMBER IN ('1','2','3')
Just to let you know, a friend of mine helped me building a query that in some way gave me what I needed.
Example:
db.table1.find({number: {$in: ['1','2','3']}}, {id: 1,'table2.mobile_phone': 1});
In Mongo 3.2 the $lookup
parameters was introduced, see the documentation
db.collection.aggregate([
{
$lookup:
{
from: "othercollection",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
For the moment the $lookup
operator is only available in the aggregation framework and you may run into problems if you try use to join big collections.
Alternatively you can join the data at application level. You would basically run two find queries and combine the results by joining them on the relevant keys.