4

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" 
        }  
    }
])
chridam
  • 95,056
  • 21
  • 214
  • 219
arunraj770
  • 607
  • 1
  • 8
  • 25

1 Answers1

2

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 be object

  • If localField type string then foreignField should be string

  • If localField type number then foreignField should be number

$lookup Documentation

Shaishab Roy
  • 15,447
  • 4
  • 47
  • 66