Home > other >  In NoSQL databases like MongoDB and Cassandra, what is the proper way to model a resource that can c
In NoSQL databases like MongoDB and Cassandra, what is the proper way to model a resource that can c

Time:10-31

For instance, let's say I am trying to design a model for a database that stores Instagram posts and that a single post can have multiple images. Let's say I'm storing images in S3. My question is how would I tie the images and the posts together?

In a standard relational Database I would probably create a separate table for images and store the foreign key of the Instagram post and the path to the image in S3. Then when I am retrieving the post, I would join on this images table.

In a NoSQL DB like MongoDB or Cassandra, my understanding is that it is better to avoid joins for the sake of latency. So would I store an array of image paths directly in my posts table?

CodePudding user response:

In mongodb case, My approach would be like this:

{
  "name":"post1",
  "content":"post1 content",
  "images":[
    "https://example.com/img1.jpg",
    "https://example.com/img2.jpg"
  ]
}

Yes, it's better to embed than to link other documents, unless you have a good reason to it.

even in case you need to fetch all images, you can query the db, and project the result to match your needs.

  • Related