1

I have a web application that uses MongoDB as primary database. This is the first project I do with a NoSQL DB. I'm trying to create the application model but I'm confused. I know NoSQL databases are non-normalized and it is normal to replicate data avoiding joins (which are not supported) but I'm now thinking of this scenario:

I have the User class containing email address and the user can change it in the profile page. This email address is displayed in a few pages in different environments and I think the right choice is to embed this property in a few class documents (ex.: product subscription, invoice, shopping cart, ... ).

What if the user changes his email address? Do I have to trigger multiple updates to change every collection, every document that embeds this property?

  • I know NoSql are non-normalized and it is normal to replicate data avoiding joins (that are not supported) -- Disregarding normalization because joins are not supported does not sound like a very good idea to me. You can simulate joins in a NoSql database; you just have to write code, that's all. Us old-timers have had joins as a native capability in our databases for ages; we wonder why anyone would ever want to do without them. – Robert Harvey Apr 4 '14 at 16:24
  • Would you change the email address associated with a paid invoice? Robert Harvey is right. You're going to have to create code to make the changes as needed? – JeffO Apr 4 '14 at 17:02
1

If i understand your question right...

For an example:

User Model: 
    {
     name: String,
     email: String
     ...
     ...
    }

other models (product subscription, invoice, shopping cart) will have ref. to User model, for example Invoice Model:

{
 name: String,
 amount: Number,
 date: Date
 ...
 ...
 _user: { type: ObjectID, ref: 'UserModel'}
}

then when you fetching data for models that have user reference you can populate _user, and have current user object into data that you are fetching from db.

You don't need to store user data in every single model only _id with reference to User Model. With this implementation you only need to update your User collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.