Home > Back-end >  Structure with more Depth vs more breadth in Mongodb overhead
Structure with more Depth vs more breadth in Mongodb overhead

Time:10-14

I'm trying to store user notifications in mongoDB ( My main database is Mysql ) and the notification table has over 2,500,000 records for each notification.

I want to know that should i store each notification in one row or store them based on user_id, each row containing all notifications of an user. Queries that will perform on these data are selecting all notifications of a user and selecting all notification with notified flag set to false and updating a flag of a notification with notification id.

Each row on table has id,user_id,description,read,notified

Which one has less overhead?

CodePudding user response:

Store each notification in one row would be recommended since query or update would be easier.

Good

_id:"1"
user_id: "1",
description: "123",
read: true

Only if notifications and fields are less each user.

_id:"1",
user_id: "1",
notifications: [ 
{
  _id:"1",
  description: "123",
  read: true
}]

CodePudding user response:

storing all user's notifications in 1 document is not a good idea,
although MongoDB can deal with nested documents well, there are some limitations on the document size, which is 16MB as MongoDB stayed, anyway it all depends on your use case, and you are the only one can decide the best scheme for your business, I would advise you to take a look on this super useful article from MongoDB Engineers and try to compare your use case with some of this design patterns
Building with Patterns: A Summary

  • Related