简体   繁体   中英

MongoDB performance

I need to design mongoDB database for save history notifications. And I consider two possible solutions.

1) every user will have 1 document like this:

{
_id: 1234567890abcdefgh,
emailfrom: email1@example.com,
history: {
  email2@example,com: {
    {
      read: 1,
      text: 'hey man',
      time: 2015-11-20-23-05-15
    },
    {
      read: 0,
      text: 'hey whats up',
      time: 2015-11-20-23-10-10
    }
  },
  email3@example,com: {
    {
      read: 1,
      text: 'oooh',
      time: 2015-11-20-23-05-11
    }
  }
}

}

2) second solution is make for every notificaion own document like this:

{
 _id: 1234567890abcd
 emailfrom: email1@example.com,
 emailto: email2@example.com,
 text: 'hey man',
 read: 1,
 time: 2015-11-20-23-05-15
},
{
 _id: 1234567890abcd
 emailfrom: email1@example.com,
 emailto: email2@example.com,
 text: 'hey whats up',
 read: 0,
 time: 2015-11-20-23-05-15
},
{
 _id: 1234567890abcd
 emailfrom: email1@example.com,
 emailto: email3@example.com,
 text: 'oooh',
 read: 1,
 time: 2015-11-20-23-05-11
},

My question is about performance of this two approaches. when there will be thousands and thousands notifications and I will want to select, update or find:

1) find all notifications send from email1@example.com AND read: 0 - I think first approach will be faster

2) I will want to save new notification - I think second approach will be faster

3) I will want to update read 0 -> read 1 emailfrom email1@example.com emailto email2@example.com - I don't know which approach would be faster.

Could anyone help me with this? Which one is the right approach for save this type of data? Thank you for any comment!

I guess there is a typo error in the first approach. The type of "email2@example,com" and "email3@example,com" should be array, right? Whatever, according to the use cases you mentioned, I prefer to choose option 2. Just add proper index to that table, such as compound index for " emailfrom + read " and " emailfrom + emailto " which can ensure you have high performance during query.

The most common case is query by emailfrom and another condition which option 1 cannot help too much. And since there will be many many notification coming in future, you still consider there is a 16M limitation for each document size in mongoDB.

If you think about thousands of notifications in the future, I strongly advice against storing embedded history of notifications in every user's document due to limitation of document size.

HERE is the article shows some approaches to designing a mongoDB schema which seems to be similar to your (inbox).

Additionally, HERE is a great, short guide from docs.mongodb.org which can help you to make decision what MongoDB data model will be the best for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM