How to structure polymorphic data

Hi,

So we are rebuilding an existing Laravel app to a new app powered by Nuxt, Nestjs, Prisma and Mongodb.

The app contains a lot of relations and I am not sure how to structure some of them because of the SQL to NoSQL translation.

Collections:

  • Users
  • Destinations
  • Spots
  • Visits
  • Tips
  • Media

The app contains destinations where users can add visits and tips etc. Visit and destination documents can contain images which we are uploading to Cloudinary. Once an image is uploaded we receive an id from cloudinary.

SQL solution
In Laravel we created a polymorphic relation where a media item would belong to a destination, spot or visit. The media item would also belong to a user thanks to the user id that was stored in the media table.

NoSQL solution
I would like to create a relation between the image and the user so we are storing a new document in the media collection that contains the Cloudinary id and the User id. This is working fine. I would also like to create a relation between the media item and the destination or visit, depending on the type. The media item would be queried from the user page and from the destination, spot or visit page.

Would it be wise to just embed the media inside the destination or visit document? We are planning on expanding the collections that use media in the future.

The same goes for the visits. A visit can belong to a destination or a spot but it also belongs to a user.
Right now I have the following structure for the collections:

Destinations

  • _id
  • name
  • slug
  • userId: ObjectId >> User
  • Spots: >> relation to Spots

Spots

  • _id
  • name
  • slug
  • userId: ObjectId >> User
  • destinationId: ObjectId >> Destination

Visits

  • _id
  • name
  • slug
  • userId: ObjectId >> User
  • typeId: ObjectId >> Destination or Spot

Media

  • _id
  • name
  • slug
  • userId: ObjectId >> User
  • typeId: ObjectId >> Destination, Spot or Visit

Let’s say I would like to query all the users images with the visit or destination relation attached. What would be the best way to structure our data?

Thanks in advance.