2

I am trying out MongoDB in correlation with ASP.NET, using the MongoDB C# driver NuGet Package. I think i have a problem with grasping one aspect of data modeling with document databases. The big question is about keeping data updated across collections.

Lets say we have two Collections, User and Post. All users will exist in the User collection. A user can create many posts, but i want the user information to be easy to select from the database, when handling posts in general. That is where i see many database designs embedding the user document in the post, aswell as having the user document in the user collection.

My question is: How do i keep the user data updated in both collections? Ie. if i would update a users age or name, how would i make sure the embedded user gets updated as well?

Another solution would be to simply store the UserId on each Post, but i just see many designs where people embed documents, and thereby create duplicate data, across collections.

This is coming from a brain that is used to thinking RDBMS, so please be gentle. The relationship between Post and User is quite simple, but i just cannot figure out how you would normally do this in an NoSQL model.

Example:

Post:

{
    "_id": "56cc5fca4b4c7818fc9363d3",
    "title": "test title",
    "User": {
        "_id": "56cc3a314b4c78191434424c",
        "name": "Thomas",
        "age": 23
    }
}

User:

{
    "_id": "56cc3a314b4c78191434424c",
    "name": "Thomas",
    "age": 23
}
Community
  • 1
  • 1
Teilmann
  • 2,146
  • 7
  • 28
  • 57

0 Answers0