在MongoDB中列出涉及用户的每次对话的最后一条消息

编程入门 行业动态 更新时间:2024-10-22 12:27:05
本文介绍了在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 }

那么我该如何从木头"用户可能进行的每次对话中检索到最后一条消息?

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.

我的数据库架构如下:

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 } });

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

proof of what kind of people run this site: i62.tinypic/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.

推荐答案

欢迎来到堆栈溢出.您发布了一个不错的问题.请让我有幸以我能提供的方式为您提供帮助.

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"} } } ])

您可以在猫鼬上运行以下内容.

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中列出涉及用户的每次对话的最后一条消息

本文发布于:2023-10-27 23:21:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   用户   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!