嵌入式文档中的mongodb限制

嵌入式文档中的mongodb限制

本文介绍了嵌入式文档中的mongodb限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个消息系统,使一个人可以与许多用户进行对话.例如,我开始与user2,user3和user4交谈,因此他们中的任何人都可以看到整个对话,并且如果对话在任何时间都不是私密的,则任何参与者都可以将任何其他人添加到对话中. >

这是我的想法.我正在使用Mongo,我的想法是将对话框用作实例而不是消息.

该架构如下:

{
_id : ...., // dialog Id
'private' : 0 // is the conversation private
'participants' : [1, 3, 5, 6], //people who are in the conversation
'msgs' :[
  {
   'mid' : ...// id of a message
   'pid': 1, // person who wrote a message
   'msg' : 'tafasd' //message
  },
  ....
  {
   'mid' : ...// id of a message
   'pid': 1, // person who wrote a message
   'msg' : 'tafasd' //message
  }
]
}

我可以看到这种方法的一些优点-在大型数据库中,可以轻松找到某些特定对话的消息.-将人们添加到对话中很容易.

但是这是一个问题,我找不到解决方案:对话太长了(以skype为例),它们没有向您显示所有对话,而是向您显示了一部分,之后又向您显示了其他消息.在其他情况下,跳过,限制可以解决问题,但是我该怎么办?

如果这不可能,您有什么建议?

解决方案

MongoDB文档解释了如何选择数组元素的子范围.

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

您可以使用此技术来仅选择与您的UI相关的消息.但是,我不确定这是一个好的架构设计.您可能要考虑从已归档"消息中分离出可见"消息.这可能会使查询更容易/更快.

I need to create a message system, where a person can have a conversation with many users.For example I start to speak with user2, user3 and user4, so anyone of them can see the whole conversation, and if the conversation is not private at any point of time any of participants can add any other person to the conversation.

Here is my idea how to do this.I am using Mongo and my idea is to use dialog as an instance instead of message.

The schema is listed as follows:

{
_id : ...., // dialog Id
'private' : 0 // is the conversation private
'participants' : [1, 3, 5, 6], //people who are in the conversation
'msgs' :[
  {
   'mid' : ...// id of a message
   'pid': 1, // person who wrote a message
   'msg' : 'tafasd' //message
  },
  ....
  {
   'mid' : ...// id of a message
   'pid': 1, // person who wrote a message
   'msg' : 'tafasd' //message
  }
]
}

I can see some pros for this approach- in a big database it will be easy to find messages for some particular conversation.- it will be easy to add people to the conversation.

but here is a problem, for which I can't find a solution:the conversation is becoming too long (take skype as an example) and they are not showing you all the conversation, they are showing you a part and afterwards they are showing you additional messages.In other situations skip, limit solves the case, but how can I do this here?

If this is impossible what suggestions do you have?

解决方案

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.

这篇关于嵌入式文档中的mongodb限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:03