Mongoose - 查找每个用户的最后一条消息
Mongoose - find last message from each user
我正在开发消息系统,我需要从每个向登录用户发送消息的用户那里获取最后一条消息。我在 mongoDB:
中有这个结构
[{
"_id": "551bd621bb5895e4109bc3ce",
"from": "admin",
"to": "user1",
"message": "message1",
"created": "2015-04-01T11:27:29.671Z"
}, {
"_id": "551bd9acf26208ac1d9b831d",
"from": "user1",
"to": "admin",
"message": "message2",
"created": "2015-04-01T11:42:36.936Z"
}, {
"_id": "551bdd6d849d53001dd8a64a",
"from": "user1",
"to": "user2",
"message": "message3",
"created": "2015-04-01T11:58:37.858Z"
}, {
"_id": "551bdd99849d53001dd8a64b",
"from": "user2",
"to": "admin",
"__v": 0,
"message": "message4",
"created": "2015-04-01T11:59:21.005Z"
}, {
"_id": "551bdda1849d53001dd8a64c",
"from": "user1",
"to": "admin",
"__v": 0,
"message": "message5",
"created": "2015-04-01T11:59:29.971Z"
}]
我需要从向登录用户发送消息的每个用户的最后一条消息中获取字段 from
、message
和 created
。我试过使用 distinct 但它 returns 只有一个字段。我有这个:
Message.find({
to: req.user.username
})
.select('message created')
.sort('-created')
.exec(function (err, messages) {
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
});
} else {
res.json(messages)
}
});
但是 returns 所有向登录用户发送消息的用户,我只需要唯一用户和他们的最后一条消息。有什么方法可以用 mongoose 做到这一点吗?
在您的管道阶段具有 $match
, $sort
, $group
and $project
表达式的地方使用聚合框架:
Message.aggregate(
[
// Matching pipeline, similar to find
{
"$match": {
"to": req.user.username
}
},
// Sorting pipeline
{
"$sort": {
"created": -1
}
},
// Grouping pipeline
{
"$group": {
"_id": "$from",
"message": {
"$first": "$message"
},
"created": {
"$first": "$created"
}
}
},
// Project pipeline, similar to select
{
"$project": {
"_id": 0,
"from": "$_id",
"message": 1,
"created": 1
}
}
],
function(err, messages) {
// Result is an array of documents
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
});
} else {
res.json(messages)
}
}
);
如果 req.user.username = "admin"
,使用您的样本集合,则结果为:
{
"result" : [
{
"message" : "message4",
"created" : "2015-04-01T11:59:21.005Z",
"from" : "user2"
},
{
"message" : "message5",
"created" : "2015-04-01T11:59:29.971Z",
"from" : "user1"
}
],
"ok" : 1
}
我正在开发消息系统,我需要从每个向登录用户发送消息的用户那里获取最后一条消息。我在 mongoDB:
中有这个结构[{
"_id": "551bd621bb5895e4109bc3ce",
"from": "admin",
"to": "user1",
"message": "message1",
"created": "2015-04-01T11:27:29.671Z"
}, {
"_id": "551bd9acf26208ac1d9b831d",
"from": "user1",
"to": "admin",
"message": "message2",
"created": "2015-04-01T11:42:36.936Z"
}, {
"_id": "551bdd6d849d53001dd8a64a",
"from": "user1",
"to": "user2",
"message": "message3",
"created": "2015-04-01T11:58:37.858Z"
}, {
"_id": "551bdd99849d53001dd8a64b",
"from": "user2",
"to": "admin",
"__v": 0,
"message": "message4",
"created": "2015-04-01T11:59:21.005Z"
}, {
"_id": "551bdda1849d53001dd8a64c",
"from": "user1",
"to": "admin",
"__v": 0,
"message": "message5",
"created": "2015-04-01T11:59:29.971Z"
}]
我需要从向登录用户发送消息的每个用户的最后一条消息中获取字段 from
、message
和 created
。我试过使用 distinct 但它 returns 只有一个字段。我有这个:
Message.find({
to: req.user.username
})
.select('message created')
.sort('-created')
.exec(function (err, messages) {
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
});
} else {
res.json(messages)
}
});
但是 returns 所有向登录用户发送消息的用户,我只需要唯一用户和他们的最后一条消息。有什么方法可以用 mongoose 做到这一点吗?
在您的管道阶段具有 $match
, $sort
, $group
and $project
表达式的地方使用聚合框架:
Message.aggregate(
[
// Matching pipeline, similar to find
{
"$match": {
"to": req.user.username
}
},
// Sorting pipeline
{
"$sort": {
"created": -1
}
},
// Grouping pipeline
{
"$group": {
"_id": "$from",
"message": {
"$first": "$message"
},
"created": {
"$first": "$created"
}
}
},
// Project pipeline, similar to select
{
"$project": {
"_id": 0,
"from": "$_id",
"message": 1,
"created": 1
}
}
],
function(err, messages) {
// Result is an array of documents
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
});
} else {
res.json(messages)
}
}
);
如果 req.user.username = "admin"
,使用您的样本集合,则结果为:
{
"result" : [
{
"message" : "message4",
"created" : "2015-04-01T11:59:21.005Z",
"from" : "user2"
},
{
"message" : "message5",
"created" : "2015-04-01T11:59:29.971Z",
"from" : "user1"
}
],
"ok" : 1
}