hutar94
hutar94

Reputation: 63

mongoDB - pros and cons of two approaches to store same data

I am coding simple fitness app to teach myself node better.

User can save informations about his lifts. To be more precise, he will insert exercise, repetitions, weight lifted and date.

I can think of two ways of storing this data.

First

{username: "username",
exercises: {
    deadlift: [{
            reps: 5,
            weight: 100
            date: some date
        },
        {
            reps: 6,
            weight: 110,
            date: somedate
        }
        ]},
    squat: [{
            reps: 5,
            weight: 100
            date: some date
        }]
}

This would just mean that every user would have his own object with his username and different exercises and different attempts of that exercise.

Second approach would be to have one big collection of attempts objects of all users like this

{   
    username: "username",
    exercise: "deadlift",
    reps: 5,
    weight: 100,
    date: somedate
}

Would one of these two ways would be better in real life scenario? If yes when and why?

Upvotes: 3

Views: 173

Answers (1)

TJ1S
TJ1S

Reputation: 528

Ultimately, I think it depends on what kinds of queries you want to run. Here are some quick thoughts:

The advantage of the first one is that for a given user, you can immediately know all of his/her exercises with a single lookup. This is great for caching as well, depending on the application. The downside to this approach is that if you have an overzealous lifter, you could end up running out of room in the document to store exercises (16mb limit if I recall correctly). Additionally, it makes it a bit more difficult for what I'd call "analytical queries" - ex, finding trends among your users, that sort of thing. Since you're growing a document each time, I don't know what the resulting disk layout would be (not as familiar with the new storage engine). If the size of the document grows each time the user exercises, this could cause some wasted space on the disk.

The advantage of the second approach is that you don't have to worry about any limits. Admittedly, this might not be a problem to become with. I'd imagine for the most part that your user enters an exercise and then doesn't touch it, so you might get around the disk problem. Additionally, it allows you to do more advanced queries, such as determining what exercises are common / popular among your users. I think it'd be better for that sort of analysis. The downside is that depending on what you choose to index on, the queries are a bit more involved in terms of what actually gets executed to find all exercises for a given user. If you index on username, and you only plan on running queries to find what exercises a user does, then you're probably fine.

I'd be more inclined to go with the second one as it seems a bit more flexible to me.

Upvotes: 1

Related Questions