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
  • 100,957
  • 23
  • 236
  • 235
arunraj770
  • 767
  • 1
  • 10
  • 29

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
  • 16,335
  • 7
  • 50
  • 68