DEADBEEF
DEADBEEF

Reputation: 2250

When to duplicate data in noSQL?

I'm working on a project where Firebase Firestore will be used. I've not had experience with noSQL so I work on understanding this technology.

The application lets an user to select a movie category and download an associated movie to let him movie edition.

The category and movie won't be change by the users and will be fixed or change by the owner. Think this like a Netflix catalog where the user can only watches the movies

There are several categories but for the moment only one movie is inside a category (may have more later). Each movie has related metadata.

In the future: - an user object will be used to rank each user regarding their score (information related to the application). - some movie will be available with localisation restriction, i.e a movie would be available for US user only.

In my first thought, the data structure will look like this:

// Collection
Category: {
    name: "drama" // Could be action, or other
}

// Collection
Movie: {
    name: "Matrix"
    description: "Best movie ever"
    duration: 1321312
    url: "https://www....."
    allowedCountry: "us" // is it the right place for this field?
    category: "drama" // is it ok to duplicate data here?
}

//Collection
user: {
    ranking: 3
    withMovie: "Matrix" // is it ok to duplicate data here?
}

I'm not sure if it's the right data structure for this problem.

The flow of the app would present in the first time all the possible categories (so I create a separated collection in order to avoid to iterate on all the song to get the categories)

Then when the user select a category the possible movies are display and the app download the selected one.

Is is okay to iterate on all possible movies to show the movies related to the category? Or should I put the movies as a sub collection of the category collection?

Upvotes: 5

Views: 3193

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

I'd typically indeed keep the category directly in the movie document in this scenario, since it makes the queries much easier to read.

firebase.firestore().collection("movies").where("category", "==", "drama")

In fact, consider if your movies can really only have a single category (as you've modeled it now), or whether they may have multiple categories in the future (as Netflix does as far as I know). You'd model the latter as an array of categories:

categories: ["drama", "sci-fi"]

And then query it with:

firebase.firestore().collection("movies").where("categories", "array-contains", "drama")

Upvotes: 2

Related Questions