消息传递应用程序的MongoDB聚合查询
我正在开发一个具有消息功能(聊天)的应用程序,我希望获得所有我拥有的用户或向我发送消息的用户,并按照最近通信的联系人到达顶部的顺序对所有这些用户进行分组。 并统计了我从另一个用户那里收到的、我还没有读过的所有信息 Mongo消息模型消息传递应用程序的MongoDB聚合查询,mongodb,mongoose,mongodb-query,aggregation-framework,aggregate,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,Aggregate,我正在开发一个具有消息功能(聊天)的应用程序,我希望获得所有我拥有的用户或向我发送消息的用户,并按照最近通信的联系人到达顶部的顺序对所有这些用户进行分组。 并统计了我从另一个用户那里收到的、我还没有读过的所有信息 Mongo消息模型 const userChat = new Schema({ senderUserId: { type: Schema.Types.ObjectId, ref: "User" }, message: { type
const userChat = new Schema({
senderUserId: {
type: Schema.Types.ObjectId,
ref: "User"
},
message: {
type: String
},
room: {
type: Array
},
files: {
type: Array
},
read: {
type: Boolean,
default: false
},
receiverUserId: {
type: Schema.Types.ObjectId,
ref: "User"
}
}, {
timestamps: true
})
我的MongoDB聚合
const user = await UserChat.aggregate([{
$match: {
$and: [{
$or: [{
receiverUserId: req.user._id
}, // User is in recepients
{
senderUserId: req.user._id
}, // or the sender
],
}, ],
},
},
{
$sort: {
createdAt: -1,
},
},
{
// if the message is send by me then i group the "$receiverUserId"
// if not that mean i am the receiver so i group the "$senderUserId"
$group: {
_id: {
$cond: {
if: {
$eq: ["$senderUserId", req.user._id]
},
then: "$receiverUserId",
else: "$senderUserId"
}
},
count: {
$sum: {
$cond: ["$read", 0, 1]
},
},
createdAt: {
$first: "$createdAt",
},
},
},
$lookup: {
from: "users",
localField: "_id",
foreignField: "_id",
as: "user",
},
}, {
$sort: {
createdAt: -1,
},
}, {
$project: {
_id: 1,
count: 1,
createdAt: 1,
user: 1,
},
}, ]);
我想要获取的数据示例
比如说,我们有两个用户互相写信。
用户1被称为帕皮·约翰逊,另一个是米歇尔王子
米歇尔王子向帕皮·约翰逊发送两条消息。
由于帕皮·约翰逊尚未阅读米歇尔亲王的信息,因此当帕皮·约翰逊请求他发送或接收的信息时,结果应该是:
{
_id: 5f8cf7e3c68dd730b5edb8a9, // the ID of Prince Michel
count: 2, // number of message unread send by Prince Michel to Papi johnson
createdAt: 2020-10-19T02:27:39.759Z, // the last message receive
user: [ [Object] ] // in then the user info of Prince Michel
}
{
_id: 5f8cf8f5b76f3e3d5e6e249f,// the ID of Papi johnson
count: 0, // number of message unread send by Papi johnson to Prince Michel
createdAt: 2020-10-19T02:27:39.759Z,
user: [ [Object] ]// in then the user info of Papi johnson
}
当米歇尔王子请求接收他发送或接收的信息时,结果应该是:
{
_id: 5f8cf7e3c68dd730b5edb8a9, // the ID of Prince Michel
count: 2, // number of message unread send by Prince Michel to Papi johnson
createdAt: 2020-10-19T02:27:39.759Z, // the last message receive
user: [ [Object] ] // in then the user info of Prince Michel
}
{
_id: 5f8cf8f5b76f3e3d5e6e249f,// the ID of Papi johnson
count: 0, // number of message unread send by Papi johnson to Prince Michel
createdAt: 2020-10-19T02:27:39.759Z,
user: [ [Object] ]// in then the user info of Papi johnson
}
这就是我得到的结果:
照片中的示例
**问题是我收到的两个用户的未读邮件数量相同。
我认为问题可能是“$group”和“$sum”查询不仅计算用户收到的未读消息,还计算用户发送或接收的所有未读消息。
我已经写了10多天的博客了,我不知道该怎么做。
**
当Papi johnson请求他发送或接收的消息时,结果是:
{
_id: 5f8cf7e3c68dd730b5edb8a9, // the ID of Prince Michel
count: 2,
// I think here the "$ group" and "$ sum"
//query does not count only unread message received by the user,
// but rather all unread message that is sent or received by the user.
createdAt: 2020-10-19T02:27:39.759Z, // the last message receive
user: [ [Object] ] // in then the user info of Prince Michel
}
{
_id: 5f8cf8f5b76f3e3d5e6e249f,// the ID of Papi johnson
count: 2,
// I think here the "$ group" and "$ sum"
//query does not count only unread message received by the user,
// but rather all unread message that is sent or received by the user.
createdAt: 2020-10-19T02:27:39.759Z,
user: [ [Object] ]// in then the user info of Papi johnson
}
当米歇尔王子请求接收他发送或接收的信息时,结果是:
{
_id: 5f8cf7e3c68dd730b5edb8a9, // the ID of Prince Michel
count: 2,
// I think here the "$ group" and "$ sum"
//query does not count only unread message received by the user,
// but rather all unread message that is sent or received by the user.
createdAt: 2020-10-19T02:27:39.759Z, // the last message receive
user: [ [Object] ] // in then the user info of Prince Michel
}
{
_id: 5f8cf8f5b76f3e3d5e6e249f,// the ID of Papi johnson
count: 2,
// I think here the "$ group" and "$ sum"
//query does not count only unread message received by the user,
// but rather all unread message that is sent or received by the user.
createdAt: 2020-10-19T02:27:39.759Z,
user: [ [Object] ]// in then the user info of Papi johnson
}
你能发布你现在得到的结果和你的期望吗?你好@wak786我做了一些更新,我希望能回答你的问题。谢谢你,我现在回答这个问题了。你能提供一些示例文档吗?这样我就可以用它们来进行查询了。您可以创建
Mongo游乐场
,并与我共享链接@这里是wak786的链接,你能发布你现在得到的结果和你的期望吗?你好@wak786我做了一些更新,我希望能回答你的问题。谢谢你,我现在回答这个问题了。你能提供一些示例文档吗?这样我就可以用它们来进行查询了。您可以创建Mongo游乐场
,并与我共享链接@wak786这里是链接