mongodb join table queries can either use mongoose"s pupulate (), or aggregate $lookup, if you use two single table queries:
//one
{
_id: "joe",
name: "Joe Bookreader"
}
//two
{
patron_id: "joe",
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
{
patron_id: "joe",
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}
db.ones.findOne({_id:"joe"))
db.twos.find({patron_id:"joe"})
when executing two single-table queries, _ id,patron_id has an index, and patron_id corresponds to the number of joe in single digits. The index granularity is very low and can Filter a lot of data. Should these two queries also be fast (both in memory)? who is more efficient than the $lookup query? Does the aggregation $lookup query do the same internally for two find queries, and whether there is an index on the foreignField corresponding key should affect the efficiency of the query?
db.ones.aggregate([
{
$lookup:
{
from: "twos",
localField: "_id",
foreignField: "patron_id",
as: "arrs"
}
},
{ $match : {_id:"joe") }
])