【发布时间】:2014-10-22 01:36:11
【问题描述】:
好的,我的数据模型基本上是这样的:
var messageSchema = new Schema({
to: { type: String, required: true},
from: { type: String, required: true},
message: { type: String, required: true}
});
我想做的是有一个功能,我可以传入当前登录用户的用户名:例如 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 个结果。
每个对话应该只提取 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、来往和来自
【问题讨论】:
-
为了澄清为什么你还没有在我给你的一个问题和其他 cmets 上被投票,你真正需要在这里解释的是为什么?意思是从你给出的样本中,为什么你指出的两个项目出来是“不同的”?如果您不接受,人们将不会回答您的问题。鉴于前者和对您的结果缺乏合理解释,人们不会浪费时间。由你自己解释。记住。忙碌的著名同事。清楚地解释自己,然后你就会学习。
标签: node.js mongodb mongoose mongodb-query