问题
table1
has a field string "value" and table2
has a field "value" as ObjectId
, Is it possible to do a query like this or how to write
table1.aggregate([
{
$lookup: {
from: "table2",
localField: "value",
foreignField: "_id",
as: "test"
}
}
])
回答1:
As far I know to join collections using $lookup
operator in MongoDB data type should be same. If type mismatch then $lookup
will not work. So to join you should use those field that are same type because it check equality.
The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection
If localField type
object
then foreignField should beobject
If localField type
string
then foreignField should bestring
If localField type
number
then foreignField should benumber
$lookup Documentation
来源:https://stackoverflow.com/questions/41393899/is-it-possible-to-compare-string-with-objectid-via-lookup