Mongoose如何获得对话列表,特定用户已与之聊天?

时间:2014-10-21 12:16:57

标签: node.js mongodb mongoose mongodb-query

好的,基本上我的数据模型如下所示:

var messageSchema = new Schema({
    to: { type: String, required: true},
    from: { type: String, required: true},
    message: { type: String, required: true}
});

我想要做的是有一个函数,我可以传入当前登录用户的用户名:example billy。

数据库可能包含100条消息,如下所示:

[{"_id":"2394290384","from":"billy","to":"dan","message":"some message"},
 {"_id":"2394290384","from":"dan","to":"billy","message":"some message"},
 {"_id":"2394290389","from":"john","to":"billy","message":"some message"},
 {"_id":"2394290389","from":"billy","to":"john","message":"some message"}] 

它应该给我一个这样的输出:

 [{"_id":"2394290384","from":"billy","to":"dan","message":"some message"},   
  {"_id":"2394290389","from":"john","to":"billy","message":"some message"}] 

如何为每条消息提取最新的1。

所以它基本上显示每个对话/我与之聊天的人的结果,无论我是否向他们发送了消息,或者他们都给我发了消息。

每次会话只需将与其最新消息相关的所有数据提取1个结果。

所以最新消息,该消息的id,用户来往和来自。

这样我就可以在一侧创建一个会话列表,在列表中显示用户及其消息。用户点击它并显示聊天。

我已经问过这个问题的多种变体试图解决这个问题,到目前为止我找不到解决方案。

我来自php mysql背景,所以我是node和mongoose的新手。

提前感谢您的帮助我真的很感激。

我已经提到为什么示例中的2个项目是不同的。它根据当前登录的用户名为每个会话提取1个结果。

所以需要有这样的功能:

getconversations(username){
Message.find(Where messages are to this username or from this from username).exec(function(error,results){
this would probably output all messages like so:
[{"_id":"2394290384","from":"billy","to":"dan","message":"some message"},
 {"_id":"2394290384","from":"dan","to":"billy","message":"some message"},
 {"_id":"2394290389","from":"john","to":"billy","message":"some message"},
 {"_id":"2394290389","from":"billy","to":"john","message":"some message"}] 


})
}

if the current username was billy.

    But I only want 1 of the latest.

    So i can have a list on the left:
    messages:
    username:
    lastmessage
    username:
    last message

    a user clicks on it and it opens up the full chat. For example like you see on skype on ios or android: it shows you a list of users and their message below. you tap on it and it shows you the chat. it is basically pulling that list based on people you have had a conversation with.

我最终如何运作:

var user="billy";
Message.aggregate(
    {
           $match: {
              $or: [{
                to: user
              }, 
              {
                from: user
              }]
            }
        },
    { $project: { "from" : {
      $cond: {'if': {$eq: ['$to',user]},'then': '$from', 'else': '$to'}
                            },
                            "to":{
      $cond: {'if': {$eq: ['$to',user]},'then': '$to', 'else': '$from'}
                            },
                            "message":"$message"



                } },
    { $sort: { _id: -1 } },
    { $group: { "id": { "$first" : "$_id" },"_id" : { "from" : "$from" }, "from": { "$first" : "$from" },"to": { "$first" : "$to" }, "message": { "$first" : "$message" }  } }
, function(err, docs) {
  res.send(docs);

});

按特定用户名进行搜索并删除所有重复项,并为每个用户输出1条记录,并将消息ID输出到

1 个答案:

答案 0 :(得分:0)

我假设问题是你如何在Mongob中使用Mongoose执行or查询?

var Message = mongoose.model('message');
Message.find({ $or: [ { from: 'billy' }, { to: 'billy' } ] }, function(err, docs) {
    // docs will now be an array of Message models
});

更新: 您可以根据插入的时间按_id排序,然后将结果限制为一条记录:

var Message = mongoose.model('message');
Message.find({ $or: [ { from: 'billy' }, { to: 'billy' } ] })
    .sort({ _id : -1 })
    .limit(1)
    .exec(function(err, docs) {
    // docs will now be an array containing 1 Message model
});

更新2:
尝试使用聚合框架:

var Message = mongoose.model('message');
Message.aggregate(
    { $project: { "from" : "$from", "message" : "$message" } },
    { $sort: { _id: -1 } },
    { $group: { "_id" : { "from" : "$from" }, "from": { "$first" : "$from" }, "message": { "$first" : "$message" } } }
, function(err, docs) {
    /* i.e. docs = array
    [ 
        { "_id" : { "from" : "alex" }, "from" : "alex", "message" : "Some message" },
        { "_id" : { "from" : "billy" }, "from" : "billy", "message" : "Some message" },
    ...

    */
});