[英]How to query next conversation sort by newest activity?
I'm implementing a chat system like Facebook, I have two tables, one is messages and one is conversations:我正在实现一个像 Facebook 这样的聊天系统,我有两个表,一个是消息,一个是对话:
conversations
id | peoples | updatedAt
1 [user1, user2] Today 8:10 PM
2 [user1, user2] Today 9:03 PM
3 [user1, user2] Today 5:01 PM
4 [user1, user2] Today 5:01 PM
5 [user1, user2] Today 5:01 PM
6 [user1, user2] Today 4:01 PM
When a user sent a message, I update the field updatedAt
of conversations
by current time so I can query the lastest conversations, but the problem here is how I continue to query the latest conversation after that?当用户发送消息时,我按当前时间更新
conversations
的字段updatedAt
以便我可以查询最新的对话,但这里的问题是我之后如何继续查询最新的对话? For example, first I will query two latest conversation, as you can see here is a conversation with id 2 and 1
will return, then I save the updatedAt
value of id 1
(the value is Today 8:10 PM
) so I can continue query latest conversation with the condition {updatedAt: {$lte: Today 8:10 PM}
and take only 2 conversations like first and it will return conversation with id 3,4
and I save the updatedAt
field of id 4
(value is Today 5:01 PM
), continue query with same condition: {updatedAt: {$lte: Today 5:01 PM}
it will return conversation with id 3,5
, the id 3
I have query before I don't need it, so how to avoid this?例如,首先我将查询两个最新的对话,如您所见,这是一个 id 为
2 and 1
的对话,将返回2 and 1
,然后我保存 id 1
的updatedAt
值(值为Today 8:10 PM
)以便我可以继续查询条件为{updatedAt: {$lte: Today 8:10 PM}
最新对话,并只进行 2 个对话,例如 first,它将返回 id 为3,4
对话,我保存 id 4
的updatedAt
字段(值为Today 5:01 PM
),以相同的条件继续查询: {updatedAt: {$lte: Today 5:01 PM}
它将返回带有 id 3,5
对话,id 3
在我不需要它之前我有查询,那么如何避免这种情况? I use mongodb as database我使用 mongodb 作为数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.