Question: In the Node.js backend environment, how do I create a relationship, using 3 collections referencing each other in a hierarchical order?
Setup
We have 3 collections, see the example docs below for the relationship:
merchant = { _id: 'merchant1' }
stampCardCampaign = { _id: 'campaign1', merchantId: 'merchant1' }
logs = { _id: 'log1', stampCardCampaignId: 'campaign1' }
We wish to create a relationship on the merchant to display all logs for all stamp cards. Documents in the logs collection do not have a reference to the merchant.
Is there a way to achieve this, without adding the merchantId to the log documents? (neither in the DB or with the addField
method)?
We are migrating from the old forest-express-mongoose
and are still learning the new Node.js way of structuring the code
Bonus question: How would I do it if the reference tree uses 4 or more collections? Before we could place our own logic in the routes, making it quite straightforward (not considering performance).
Context
- Project name: PayAtt Internal Admin Portal
- Team name: All teams
- Environment name: All environments
- Agent (old):
forest-express-mongoose: ^8.7.9
- Agent (new):
"@forestadmin/agent": "^1.13.4",
&"@forestadmin/datasource-mongoose": "^1.4.2"
- Database type: MongoDB Atlas