0

I am migrating the db from mysql to mongo, so I need to create a view in mongo, for that purpose, I need to use the left outer joins, which is $lookup in mongo. But, I want to join something like

LEFT JOIN `contracts` `con` ON ((`c`.`company_id` = con`.`company_id`)))
LEFT JOIN `contracts` `con2` ON (((`c`.`company_id`=`con2`.`company_id`)
AND (`con`.`contract_id` < `con2`.`contract_id`))))

Is it possible to use $lookup twice against the same collection? I saw some queries which uses $lookup more than once in the aggregate function, but it uses against different collections.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
RKA
  • 308
  • 2
  • 10
  • 2
    Nothing stopping you doing that. The `collection` argument to `$lookup` will just accept any value which is a valid collection ( invalid would simply return no result ). I really suggest just trying it. If you have further problems, then please present a small sample of documents from each collection and the result you expect to achieve. It's quite possible that what you want to do can be done differently than what you know as the current SQL approach. – Neil Lunn Oct 04 '19 at 10:32
  • @NeilLunn, my issue is with this compariosn, AND (`con`.`contract_id` < `con2`.`contract_id`) – RKA Oct 04 '19 at 11:29

0 Answers0