MongoDB和尚collection姓名:UserX + UserY
MongoDB monk collection name: UserX + UserY
使用前端 Vue.js 我们(用户)尝试加入私人聊天。
使用 MongoDB 将消息存储在 collection "XY" 中,
其中:
- X - UserX._id (req.user._id)
- Y - UserY._id(toUser._id - 从用户列表中选择的用户)
=> collection 名称“UserX+UserY”,类似 5ef630dfbe431b28e0d97823 + 5ef4fa80c3a5071e2055b507 = 5ef630dfbe431b28e0d978235ef4fab55b50[7152a5] =]
-前端显示用户列表(通过从数据库中读取用户名帐户)
问题:
UserX 通过选择(单击)一个 UserY 开始聊天 - MongoDB collection "UserX+UserY" 是已创建。
UserY 点击 UserX - MongoDB collection “UserY+UserX”
当 UserY 尝试加入聊天时,我需要先获取已经存在的房间,让 UserY 加入,而不是创建另一个聊天。
从 roomName(collection) 反转用户 ID 就是问题所在。
我试图通过按字典顺序比较 collection 名称中的子字符串来解决。
如果 collection 包含 BOTH 子字符串(用户 ID),我需要让用户加入 room.The 子字符串顺序并不重要。
我将使用网络套接字让用户阅读 real-time 中的消息。 Post 请求仅通过常规 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.
}
}
我建议简化您的房间命名代码。您可以让两个用户在不检查数据库的情况下选择相同的房间名称。因此,从顶部开始,您的代码可能是:
if(toUserId !== myUserId) {
let roomName = toUserID < myUserId ? toUserId+myUserId : myUserId+toUserId;
现在,两个用户都可以检查数据库以查看该房间名称是否必须创建或已经存在,发出代码中引用的“加入”事件和 post 消息。
使用前端 Vue.js 我们(用户)尝试加入私人聊天。 使用 MongoDB 将消息存储在 collection "XY" 中, 其中:
- X - UserX._id (req.user._id)
- Y - UserY._id(toUser._id - 从用户列表中选择的用户)
=> collection 名称“UserX+UserY”,类似 5ef630dfbe431b28e0d97823 + 5ef4fa80c3a5071e2055b507 = 5ef630dfbe431b28e0d978235ef4fab55b50[7152a5] =]
-前端显示用户列表(通过从数据库中读取用户名帐户)
问题:
UserX 通过选择(单击)一个 UserY 开始聊天 - MongoDB collection "UserX+UserY" 是已创建。
UserY 点击 UserX - MongoDB collection “UserY+UserX”
当 UserY 尝试加入聊天时,我需要先获取已经存在的房间,让 UserY 加入,而不是创建另一个聊天。
从 roomName(collection) 反转用户 ID 就是问题所在。
我试图通过按字典顺序比较 collection 名称中的子字符串来解决。 如果 collection 包含 BOTH 子字符串(用户 ID),我需要让用户加入 room.The 子字符串顺序并不重要。
我将使用网络套接字让用户阅读 real-time 中的消息。 Post 请求仅通过常规 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. } }
我建议简化您的房间命名代码。您可以让两个用户在不检查数据库的情况下选择相同的房间名称。因此,从顶部开始,您的代码可能是:
if(toUserId !== myUserId) {
let roomName = toUserID < myUserId ? toUserId+myUserId : myUserId+toUserId;
现在,两个用户都可以检查数据库以查看该房间名称是否必须创建或已经存在,发出代码中引用的“加入”事件和 post 消息。