我正在设计像twitter这样的社交网络系统,用户会收到通知,我会继续向mongodb发送通知。我们最多会为每个用户保留100个通知,唯一的字段是更新read
(用户已阅读通知)。所以我想知道使用多文件作为打击
{"userId":1, "content":"some msg", "read":true..}
{"userId":1, "content":"another msg", "read":true..}
或使用这样的嵌入式文档:
{"userId":1, "noficications":[{"content":"some msg", "read": true},{"content":"another msg", "read":true}]}
答案 0 :(得分:0)
我在创建我的应用程序时遇到了同样的疑问。但就我而言,对应用程序的关注是记录在子文档中的值。在某些情况下,我的代码变得不清楚。我在编写查询时遇到了一些问题。
如果您关注的是#34;用户",我在第二种方法中看不到任何问题,但如果重点是"通知",我建议您创建一个文档保存通知。
答案 1 :(得分:0)
It totally depends on your usage, If you're always going to load up most/all of the notifications for a given user then I'd keep it in one document with an array on notifications. This will allow the whole document to be mapped in to memory and will keep things snappy. Also you won't have to create any extra indexes.
{"_id":1 /* User Id */, "noficications":[{"content":"some msg", "read": true},{"content":"another msg", "read":true}]}
However if you are going to be pulling back adhoc individual notification, I'd split it down in to seperate documents, as you won't want to be keeping the whole document hot in memory. for this apporach you might end up creating another index on "userId" so you can pull back all notifications at once.
{"_id": 1 /* Notification Id */, "userId":1, "content":"some msg", "read":true..}
{"_id": 2 /* Notification Id */, "userId":1, "content":"another msg", "read":true..}
There's pros/cons to both and it all depends on your usage of data.