mongodb limit in the embedded document

偶尔善良 提交于 2019-12-03 07:16:28
jmacinnes

The MongoDB docs explain how to select a subrange of an array element.

db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: 5}}) // first 5 comments
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: -5}}) // last 5 comments
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: [20, 10]}}) // skip 20, limit 10
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: [-20, 10]}}) // 20 from end, limit 10

You can use this technique to only select the messages that are relevant to your UI. However, I'm not sure that this is a good schema design. You may want to consider separating out "visible" messages from "archived" messages. It might make the querying a bit easier/faster.

There are caveats if your conversation will have many many messages:

  1. You will notice significant performance reduction on slicing messages arrays as mongodb will do load all of them and will slice the list before return to driver only.
  2. There is document size limit (16MB for now) that could be possibly reached by this approach.

My suggestions is:

  1. Use two collections: one for conversations and the other for messages.
  2. Use dbref in messages to conversation (index this field with the message timestamp to be able to select older ranges on user request).
  3. Additional use separate capped collection for every conversation. It will be easy to find it by name if you build it like "conversation_"

Result:

  • You will have to write all messages twice. But into separate collections which is normal.
  • When you want to show your conversation you will need just to select all the data from one collection in natural sort order which is very fast.
  • Your capped collections will automatically store last messages and delete old.
  • You may show older messages on the user request by querying main messages collection.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!