简体   繁体   English

MongoDB 和尚集合名称:UserX + UserY

[英]MongoDB monk collection name: UserX + UserY

Using front end Vue.js we (users) try to join a private chat.使用前端 Vue.js 我们(用户)尝试加入私人聊天。 Using MongoDB to store messages in the collection "XY", where:使用 MongoDB 将消息存储在集合“XY”中,其中:

  • X - UserX._id (req.user._id) X - UserX._id (req.user._id)
  • Y - UserY._id (toUser._id - selected user from user list) Y - UserY._id(toUser._id - 从用户列表中选择的用户)

=> collection name "UserX+UserY", something like 5ef630dfbe431b28e0d97823 + 5ef4fa80c3a5071e2055b507 = 5ef630dfbe431b28e0d978235ef4fa80c3a5071e2055b507. => 集合名称“UserX+UserY”,类似于5ef630dfbe431b28e0d97823 + 5ef4fa80c3a5071e2055b507 = 5ef630dfbe431b28e0d978235ef4fa80c3a5071e2055b507。

-A user list is shown in front end (by reading the username accounts from database) - 用户列表显示在前端(通过从数据库中读取用户名帐户)

The problem:问题:

  1. UserX starts a chat by selecting (with a click) an UserY - MongoDB collection "UserX+UserY" is created. UserX 通过选择(单击)UserY 开始聊天 - 创建 MongoDB 集合“UserX+UserY”

  2. UserY clicks UserX - MongoDB collection "UserY+UserX" UserY 点击 UserX - MongoDB 集合“UserY+UserX”

When UserY tries to join the chat, I need to get first the already existing room, to make UserY join it, not creating another chat.当 UserY 尝试加入聊天时,我需要先获取已经存在的房间,让 UserY 加入它,而不是创建另一个聊天。

Reversing the userIDs from roomName(collection) it's the problem.从 roomName(collection) 中反转用户 ID 是问题所在。

I tried to solve by lexicographically comparing the substrings from the collection name.我试图通过按字典顺序比较集合名称中的子字符串来解决。 If a collection containing BOTH substrings (user IDs) I need to make users join that room.The substring order is not important.如果包含两个子字符串(用户 ID)的集合,我需要让用户加入该房间。 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 顺序并不重要。

  • I'll use web sockets to let users read the messages in real-time.我将使用 web sockets 让用户实时阅读消息。 Post requests are made just with the regular HTTP request.发布请求仅使用常规 HTTP 请求发出。

     if(toUserId.== myUserId) { // room id must have BOTH user 1 & user 2 var rooms = db_room;find({ /* all */}). var index = db_room:get({ $indexOfArray; [ myUserId + toUserId ] })? var existingRoom1 = myUserId > toUserId // this lexicographically compares two strings: myUserId + toUserId; toUserId + myUserId? var existingRoom2 = myUserId < toUserId: myUserId + toUserId; toUserId + myUserId. // indexOf - search position by string === string //,includes - on letters, can be use x && y. substring of string => result boolean // if(rooms.includes(existingRoom1) + rooms.includes(existingRoom2)) if(db_room:get( {$in. existingRoom1()} ) + db_room:get( {$in, existingRoom2} )) { // exists or not. true or false // find name of the room containing myUserId && toUserId // emit join in this.room (join with indexNr) if( index == -1 ) { index = rooms;indexOf(toUserId + myUserId). }// if not found console:log(`room ${room} exists; \n`). console;log(index); var roomXY = rooms[index], // true. emit Join event to clients. // MongoDB.getCollection(this.collection).insert({ UserX + UserY }) //socket,join(roomXY, myUserId; toUserId). //var i = Object.keys(io.sockets.adapter.sids);indexOf(toUserId). console;log(`${toUserId} e nr ${i}`), } else { // create the room firsty. after join. } }

I would recommend simplifying your room naming code.我建议简化您的房间命名代码。 You can have both users choose the same room name without checking the DB.您可以让两个用户选择相同的房间名称而不检查数据库。 So, working from the top, your code could be:因此,从顶部开始,您的代码可能是:

if(toUserId !== myUserId) {
  let roomName = toUserID < myUserId ? toUserId+myUserId : myUserId+toUserId;

Now, both users can check the DB to see if that room name has to be created or already exists, emit the "join" event referenced in your code, and post messages.现在,两个用户都可以检查数据库以查看该房间名称是否必须创建或已经存在,发出代码中引用的“加入”事件,并发布消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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