Ok, so basically my data model looks like this:
基本上我的数据模型是这样的
var messageSchema = new Schema({
to: { type: String, required: true},
from: { type: String, required: true},
message: { type: String, required: true}
});
What I want to do is have a function I can just pass in the username of the current logged in user: example billy.
我要做的是有一个函数,我可以传入当前登录用户的用户名:例如billy。
The database might contain 100's of messages like this:
数据库可能包含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"}]
It should just give me an output like this:
它应该给我这样的输出:
[{"_id":"2394290384","from":"billy","to":"dan","message":"some message"},
{"_id":"2394290389","from":"john","to":"billy","message":"some message"}]
How can I just pull the latest 1 for each of those messages.
我如何才能为每条消息提取最新的1。
So it basically shows 1 result per conversation / person I have had a chat with regardless of if I sent them a message or they sent me a message.
所以它基本上显示了每个对话/我与之交谈过的人的一个结果,不管我是给他们发信息还是他们给我发信息。
It should just pull up 1 result per conversation with the all the data related to their latest message.
它应该只在每次对话中拉出1个结果,与所有与他们最新消息相关的数据。
So the latest message, the id for that message, the user it was to and from.
所以最新的消息,消息的id,它的用户。
So that I can create a conversation list on 1 side which shows the users and their message below their name. the user clicks on it and it shows the chat.
这样我就可以在一边创建一个对话列表,其中显示用户和他们的消息在他们的名字下面。用户点击它,它就会显示聊天。
I have asked multiple variations of this question trying to figure it out and I have been un successful so far in finding a solution for this.
我问了很多不同的问题想要弄清楚这个问题,到目前为止我还没有找到解决这个问题的方法。
I come from a php mysql background so I am new to node and mongoose.
我来自php mysql背景,所以我对node和mongoose很陌生。
Thank you for all your help in advance I really appreciate it.
提前谢谢你的帮助,我真的很感激。
I have already mentioned why the 2 items in the example are distinct. It is pulling 1 result per conversation based on the current logged in username.
我已经提到了为什么示例中的两项是不同的。它根据当前登录的用户名为每个会话提取一个结果。
So there needs to be a function like this:
所以需要有这样一个函数
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.
How I finally got it working:
我如何让它工作:
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);
});
It searches by a specific username and strips all duplicates and just outputs the 1 record per user and outputs their message, id, to and from
它根据特定的用户名进行搜索,并删除所有重复的内容,然后输出每个用户的1条记录,并将它们的消息id输出到或输出到
1 个解决方案
#1
0
I assume the question is how do you perform an or
query in Mongodb from Mongoose?
我认为问题是如何在Mongodb中执行一个或查询?
var Message = mongoose.model('message');
Message.find({ $or: [ { from: 'billy' }, { to: 'billy' } ] }, function(err, docs) {
// docs will now be an array of Message models
});
Update: You can order by _id
as its based on time inserted and then limit results to one record:
更新:您可以根据插入的时间按_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
});
Update 2:
Try using the aggregation framework:
更新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" },
...
*/
});
#1
0
I assume the question is how do you perform an or
query in Mongodb from Mongoose?
我认为问题是如何在Mongodb中执行一个或查询?
var Message = mongoose.model('message');
Message.find({ $or: [ { from: 'billy' }, { to: 'billy' } ] }, function(err, docs) {
// docs will now be an array of Message models
});
Update: You can order by _id
as its based on time inserted and then limit results to one record:
更新:您可以根据插入的时间按_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
});
Update 2:
Try using the aggregation framework:
更新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" },
...
*/
});