Javascript 使用mongodb创建带有排序和分页的动态联系人聊天列表
我需要使用nodejs和mongodb建立一个聊天室。在这次聊天中,我需要通过订购最新的帖子和每页应用15个项目来装载联系人列表。我想知道如何使用mongodb/mongoose动态地做到这一点 例如,在第一页上,我查找由最后发送消息的人排序的联系人。如果加载第二个页面,并且有任何新消息到达,聊天列表的顺序可能已经改变 我该如何利用我的疑问进行这种治疗 我的用户模式是:Javascript 使用mongodb创建带有排序和分页的动态联系人聊天列表,javascript,node.js,mongodb,mongoose,mongodb-query,Javascript,Node.js,Mongodb,Mongoose,Mongodb Query,我需要使用nodejs和mongodb建立一个聊天室。在这次聊天中,我需要通过订购最新的帖子和每页应用15个项目来装载联系人列表。我想知道如何使用mongodb/mongoose动态地做到这一点 例如,在第一页上,我查找由最后发送消息的人排序的联系人。如果加载第二个页面,并且有任何新消息到达,聊天列表的顺序可能已经改变 我该如何利用我的疑问进行这种治疗 我的用户模式是: var schema = new Schema({ name: {type: String, required: tr
var schema = new Schema({
name: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: true, select: false},
created_at: {type: Date, required: true, default: Date.now}
});
var schema = new Schema({
content: {type: String, required: true},
type: {type: String, required: true, default: 'text'},
status: {type: String, default: 'not_read'},
created_at: {type: Date, default: Date.now},
read_at: {type: Date},
userFrom: {type: Schema.Types.ObjectId, ref: 'User', required: true},
userTo: {type: Schema.Types.ObjectId, ref: 'User', required: true}
});
我的消息架构是:
var schema = new Schema({
name: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: true, select: false},
created_at: {type: Date, required: true, default: Date.now}
});
var schema = new Schema({
content: {type: String, required: true},
type: {type: String, required: true, default: 'text'},
status: {type: String, default: 'not_read'},
created_at: {type: Date, default: Date.now},
read_at: {type: Date},
userFrom: {type: Schema.Types.ObjectId, ref: 'User', required: true},
userTo: {type: Schema.Types.ObjectId, ref: 'User', required: true}
});
先谢谢你
编辑1:
var itensPerPage = 15;
var skip = page !== undefined ? page * itensPerPage : 0;
pages = Math.ceil(pages / itensPerPage);
Message
.aggregate([
{ '$sort': {
'created_at': -1
}},
{ "$skip": skip },
{ "$limit": itensPerPage },
{ '$match': {
$or: [
{ userFrom: psychologist.id_user },
{ userTo: psychologist.id_user }
]
}},
{ '$group': {
'_id': {
'userFrom': '$userFrom',
'userTo': '$userTo'
},
}
},
])
.exec(function (err, id_users) {
if(err){
callback(new Error("Something went wrong while trying to agregate the psychologist users."), null);
}else{
var users_from_id_map = id_users.map(function(x) { return x._id.userFrom} );
var users_to_id_map = id_users.map(function(x) { return x._id.userTo} );
var chatlist = [];
var received = users_from_id_map.length;
for(var i = 0; i < users_from_id_map.length; i++){
Message
.findOne({$or : [{userFrom: users_from_id_map[i], userTo: users_to_id_map[i]}]})
.sort('-created_at')
.exec(function (err, message) {
if(err){
callback(new Error("Something went wrong while trying to find last message."), null);
}else{
if(message){
var user_chat_id;
if(psychologist.id_user.equals(message.userFrom)){
user_chat_id = message.userTo;
}else{
user_chat_id = message.userFrom;
}
var is_mine = false;
if(message.userFrom.equals(psychologist.id_user)){
is_mine = true;
}
chatlist.push({
id_user: user_chat_id,
lastMessage: {
content: (message.content.length > 35 ? message.content.substring(0,35)+"..." : message.content),
date: message.created_at,
is_mine: is_mine
}
});
}else{
chatlist.push({
id_user: user_chat_id,
lastMessage: null
});
}
received--;
if(received == 0){
next(err, psychologist, chatlist, pages);
}
}
});
}
}
});
var-itensPerPage=15;
var skip=页面!==未定义?第*页信息单位:0;
pages=Math.ceil(pages/itenserpage);
消息
.合计([
{'$sort':{
“已在处创建”:-1
}},
{“$skip”:skip},
{“$limit”:itenserpage},
{$match':{
$or:[
{userFrom:psycholister.id_user},
{userTo:psycholister.id_user}
]
}},
{'$group':{
“\u id”:{
“userFrom”:“$userFrom”,
“userTo”:“$userTo”
},
}
},
])
.exec(函数(错误、id\u用户){
如果(错误){
回调(新错误(“试图拒绝心理学家用户时出错”)、null;
}否则{
var users\u from\u id\u map=id\u users.map(函数(x){return x.\u id.userFrom});
var users_to_id_map=id_users.map(函数(x){return x._id.userTo});
var chatlist=[];
接收到的var=用户\u来自\u id\u map.length;
对于(var i=0;i35?message.content.substring(0,35)+“…”:message.content),
日期:message.created_在,
是我的吗
}
});
}否则{
chatlist.push({
id\u user:user\u chat\u id,
最后消息:空
});
}
收到--;
如果(接收==0){
接下来(呃,心理学家,聊天列表,页面);
}
}
});
}
}
});
在MongoDB中可以通过各种方式实现分页
您可以使用limit
和offset
,尽管这在请求更高的页面时会变得很慢
另一种方法是使用greaterThan
和limit
一起使用上一个联系人,比如说,获得接下来的15个联系人
不一致性(如果在分页期间发送新消息)很难防止
也就是说,MongoDB可能不是实时聊天的好选择,因为它不支持流式查询。你能给我举个例子吗?我将用我的起点更新帖子描述。谢谢