[英]How to form an unordered key with many elements in mongodb
I'm attempting to use mongodb to implement a simple messaging system between two users in mongo. 我正在尝试使用mongodb在mongo中的两个用户之间实现一个简单的消息传递系统。 I want to be able to take two users, user0 and user1, and search for their entry in a collection.
我希望能够接受两个用户user0和user1,并在集合中搜索他们的条目。 If the entry for those two users doesn't exist I want to create it and then add the message that was sent to its message field.
如果这两个用户的条目不存在,我想创建它,然后将已发送的消息添加到其消息字段中。 If it does exist I just want to push the message to the message field.
如果确实存在,我只想将消息推送到消息字段。
I'm not really sure the best way to implement this. 我不太确定实现此目标的最佳方法。
db.privateChat.update(
{between:{$all:['user0', 'user1']}},
{$push:{message:'text'}}, {upsert:true}
)
And other similar entry schemes but they don't work. 和其他类似的入门方案,但它们不起作用。 They produce the error:
他们产生错误:
"Cannot create base during insert of update. Caused by :ConflictingUpdateOperators Cannot update 'between' and 'between' at the same time"
I can think of other ways to do this producing a symmetric key (where the order of the users don't matter for the purposes of the search) from say adding the hashes together or a query that checks if either messenger0 or messenger1 is either user0 or user1 but these don't seem like great ways of doing it. 我可以想到其他方法来生成对称密钥(通过将哈希加在一起或查询来检查messenger0或messenger1是否为user0)来生成对称密钥(其中用户的顺序对于搜索而言无关紧要)或user1,但这些似乎并不是很好的方法。 Is this totally the wrong approach?
这是完全错误的方法吗?
Thanks. 谢谢。
I think this could be solved by design. 我认为可以通过设计解决。 let say that we have document in collection chats;
假设我们在聊天室中有文档;
chat{
_id,
between[arrayOfIds],
startTime,
events[
{message:{
fromUserId,
timeStamp,
data}
}}
]}
}
then messages will be stored in message object inside chat . 然后消息将存储在chat的消息对象中。
App will be aware of chat _id so there will be no issues when you will have a group chat between more than 2 users. 应用程序将知道chat _id,因此当您在2个以上的用户之间进行群聊时不会有任何问题。
This approach will allow you to prevent overflowing document size limitation as you could start new chat entry every week, day, etc... 这种方法将使您避免溢出文档大小限制,因为您可以每周,每天等开始新的聊天条目。
Have a fun! 玩得开心!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.