列出 MongoDB 中涉及用户的每个对话的最后一条消息 [英] Listing the last message of each conversation, involving an user, in MongoDB

查看:27
本文介绍了列出 MongoDB 中涉及用户的每个对话的最后一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 mongo 消息数据库,如下所示:

Lets Say I have a mongo database of messages that looks like this:

{
  "_id": ObjectId("5458009c1ab2354c029d7178"),
  "to": "dan",
  "from": "wood",
  "message": "hi dan how are you?",
  "time": new Date(1415053468590),
  "__v": 0
}
{
  "_id": ObjectId("545800b45eaf364d026c1cba"),
  "to": "wood",
  "from": "dan",
  "message": "hi wood how are you?",
  "time": new Date(1415053492125),
  "__v": 0
}
{
  "_id": ObjectId("5458009c1ab2354c029d7178"),
  "to": "billy",
  "from": "wood",
  "message": "hi billy how are you?",
  "time": new Date(1415053468590),
  "__v": 0
}
{
  "_id": ObjectId("545800b45eaf364d026c1cba"),
  "to": "wood",
  "from": "billy",
  "message": "hi wood how are you?",
  "time": new Date(1415053492125),
  "__v": 0
}

那么如何从用户wood"可能拥有的每个对话中检索最后一条消息?

So how can I retrieve the last message from each conversation that the user "wood" may have?

其他人已经发布了一个关于如何在 mysql 中执行此操作的类似问题.我在问如何在猫鼬中做到这一点.

Someone else has already posted a similar question on how to do this in mysql. I am asking how to do this in mongoose.

我会将其发布以供参考,以防万一它有所帮助:私人消息系统.列出每个会话的最后一条消息

I will post that for reference incase it helps: Private messaging system. Listing last message of each conversation

如果你能帮我一下就好了.谢谢您的帮助.对此,我真的非常感激.我是 mongoose、mongodb 和 node.js 的新手.我来自 php mysql 背景.

Would be great if you could help me out. Thank you for your help. I really appreciate it. I am new to mongoose,mongodb and node.js. I come from a php mysql background.

如果您能发布具体的操作代码,我就可以试用并提供反馈,那就太好了.谢谢.

Would be great if you could post actual code of how to do it so I can try it out and provide feedback. Thanks.

我的数据库架构如下所示:

My database schema looks like this:

var messageSchema = new Schema({
to: { type: String, required: true},
from: { type: String, required: true},
message: { type: String, required: true},
time : { type : Date, default: Date.now }
});

证明什么样的人经营这个网站:http://i62.tinypic.com/bbntx.jpg

proof of what kind of people run this site: http://i62.tinypic.com/bbntx.jpg

离开这个网站,因为你可以知道什么样的不友好的人在运行这个网站.我可能会问其他地方.他们实际上并不关心帮助你.他们关心他们的规则.你是一个新人,他们不欢迎你.我会在任何其他地方受到欢迎,而不会像这里的人们那样被当作垃圾对待.

Leaving this website as you can tell what kind of unfriendly people are running this site. Ill probably ask somewhere else. They don't actually care about helping you. They care about their rules. Your a new person they don't welcome you. Id be welcomed anywhere else not treated like dirt like people do here.

推荐答案

欢迎使用 Stack Overflow.这是一个体面的问题,你张贴.请让我有幸尽我所能帮助您.

Welcome to Stack overflow. Its a decent question, you posted. Please let me take the privilege to help you in the way i can.

这是一个可以在 mongo shell 中运行的聚合命令.请找到内嵌的解释.

This is an aggregation command which can be run in the mongo shell. Please find the explanation inline.

db.collection.aggregate([
//match all those records which involve Wood.
{$match:{$or:[{"to":"wood"},{"from":"wood"}]}},
// sort all the messages by descending order
{$sort:{time:-1}},
{
    // Now we need to group messages together, based on the to and from field.
    // We generate a key - "last_message_between", with the value being a concatenation
    // result of the values in to and from field.
    // Now, Messages from Wood to billy and Billy to wood should be treated under a single group right.
    // To achieve that, we do a small trick to make the result contain the name coming last
    // alphabetically, first. So our key for all the Messages from Wood to Billy and Billy to Wood would be 
    // "Wood and Billy".
    // And then we just display the first document that appears in the group, that would be the 
    // latest Message.
    $group:{"_id":{
    "last_message_between":{
        $cond:[
            {
                $gt:[
                {$substr:["$to",0,1]},
                {$substr:["$from",0,1]}]
            },
            {$concat:["$to"," and ","$from"]},
            {$concat:["$from"," and ","$to"]}
        ]
    }
    },"message":{$first:"$$ROOT"}
    }
}
])

您可以在 mongoose 上运行以下代码.

You can run the below on mongoose.

Collection.aggregate(
{$match:{$or:[{"to":"wood"},{"from":"wood"}]}},
{$sort:{time:-1}},
{
    $group:{"_id":{
    "last_message_between":{
        $cond:[
            {
                $gt:[
                {$substr:["$to",0,1]},
                {$substr:["$from",0,1]}]
            },
            {$concat:["$to"," and ","$from"]},
            {$concat:["$from"," and ","$to"]}
        ]
    }
    },"message":{$first:"$$ROOT"}
    }
},
function(err, res)
{
    if (err) return handleError(err);
    console.log(res);
}
)

这篇关于列出 MongoDB 中涉及用户的每个对话的最后一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆