Fetch Data from either of the collections


I have a primary collection “collection1” and an archived Collection “collection2”. Both contain similar data. “collection2” contains data that is older than 3 years. Latest 3 years of data is available in “collection1”. The application queries “collection1” alone. Is there a way where we can internally search “collection2” incase data does not exist in “collection1” within MongoDB rather than modifying the application code.

Scenario - The application queries for certain data from collection1. If data does not exist in that collection, there has to be an internal mechanism to query collection2 and fetch it to the application.
Any suggestions would be appreciated. Thank you.

Hi @Satyanarayana_Ettamsetty1,

What you describe doesn’t exist.

You could implement this logic in the back end without too much effort if that’s an option.

Another solution that comes to mind would be to use the $unionWith but this would mean that you always look for matching docs in both collections even if there are results in collection1.

If you are using Atlas Online Archive then Data Federation could also help with this use case.


1 Like