Aggregate Across Databases

Hi,
I have multiple databases In MongoDB that each of them has its collections such as:

Users Database has these collections:
1. usersInfo 2.usersReq
OR
Licenses Database has these collections:
1. licenseInfo 2.licenseHistories

and …

one of the basically Qry s for me is:
How can I use Aggregation ben multiple Databases without populate or after/befor populate?

Do you have any idea to do this?

stackoverflow Link

Hi @mehdi_parastar and welcome,

As of current version of MongoDB (v4.4), there is no way to perform cross-database lookup operation. There is an open ticket to track this request SERVER-34935, please feel free to upvote or add yourself as a watcher to receive notification updates on the ticket progress.

I’d suggest to redesign the schema. At least to merge the two databases into one.

Also, I’d suggest to design the schema based on how your application will process the data. Currently it looks like it’s based on entities, i.e. usersInfo, usersReq, licenseInfo, etc. Depending on your use case, you may be able to embed some of the information. I’d highly recommend to review Building with patterns: A summary as a good reference starter.

Regards,
Wan

1 Like