Is it possible to compare a string with ObjectId via $ lookup

table1has a string "value" of the field and table2has a field of "value" like ObjectId, is it possible to make such a request or how to write

table1.aggregate([
    { 
        $lookup: { 
            from: "table2", 
            localField: "value", 
            foreignField: "_id", 
            as: "test" 
        }  
    }
])
+4
source share
1 answer

As far as I know, to combine collections using an operator $lookupin MongoDB, the data type must be the same. If the type mismatch then $lookupwill not work. Thus, to join, you must use those fields that are of the same type, because it checks for equality .

$lookup ""

  • localField type object, foreignField object

  • localField type string, foreignField string

  • localField type number, foreignField number

$lookup

+1

Source: https://habr.com/ru/post/1665305/


All Articles