用户发送消息时,会生成一个messageTrackingId。现在,它以$ unwinding creatorName作为收件箱中唯一的返回值。我只需要一个用户条目。没有相同用户的重复项。当前,如果其他用户未响应,则他们可以发送多条消息,从而生成新的messageTrackingId。如何使初始发送的消息也出现在收件箱中,以便可以使用该messageTrackingId而不是生成新消息?我已经坚持了一段时间,因此感谢您的帮助。
app.get
app.get('/api/messages', (req, res, next) => {
query = {};
inbox = false;
messageId = false;
if (req.query.recipientId) {
query = { recipientId: req.query.recipientId }
inbox = true;
Messages.aggregate([
{
$match: {
$or: [ { recipientId: req.query.recipientId }, { creator: req.query.recipientId } ]
}
},
{
$addFields: {
conversant: {
$cond: [ { $ne: [ "$recipientId", req.query.recipientId ] }, "$recipientId", "$creator" ]
}
}
},
{
$sort: { creationDate: 1 }
},
{
$group: {
_id: "$conversant",
message: { $first: "$message" },
recipientId: { $first: "$recipientId" },
creator: { $first: "$creator" },
messageTrackingId: { $first: "$messageTrackingId" },
creationDate: { $first: "$creationDate" }
}
},
{
$lookup: {
from: "users",
localField: "creator",
foreignField: "_id",
as: "creatorName",
pipeline: [
{
$project: {
_id: 1,
message: { $arrayElemAt: ["$message", 0] },
recipientId: { $arrayElemAt: ["$recipientId", 0] },
creator: { $arrayElemAt: ["$creator", 0] },
messageTrackingId: { $arrayElemAt: ["$messageTrackingId", 0] },
creatorName: { $arrayElemAt: ["$creatorName", 0] },
}
}
],
as: 'messageTest'
},
}
])
//.populate('creator', 'username')
.then(documents => {
if (res.subject === "Test") {
}
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
} else if (req.query.creator) {
query = { creator: req.query.creator };
inbox = false;
Messages.find(query)
.populate("creator", "username")
.then(documents => {
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
} else if (req.query.messageId) {
query = { messageTrackingId: req.query.messageId };
messageId = true;
Messages.find(query)
.populate("creator", "instagramName")
.then(documents => {
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
}
});
app.post
app.post("/api/messages", checkAuth, (req, res, next) => {
console.log("Made It")
messagingTrackingIDValue = "";
const messaging = new Messages({
creator: req.userData.userId,
recipient: req.body.recipient,
recipientId: req.body.recipientId,
message: req.body.message,
//message: req.body.message,
messageTrackingId: req.body.messageTrackingId,
creatorName: req.userData.username,
creationDate: req.body.creationDate
});
//saves to database with mongoose
messaging.save().then(result => {
if (result.creator !== messaging.creator) {
} else if (result.creator === req.userData.userId) {
}
console.log(result);
res.status(201).json({
message: "Message Sent Successfully!",
postId: result._id
});
});
});
角度服务
sendMessage(
recipient: string,
message: string,
creationDate: Date,
recipientId: string,
creatorName: string,
messageTrackingId: string
) {
const messaging: Messages = {
id: null,
recipient: recipient,
message: message,
creationDate: creationDate,
creator: null,
recipientId: recipientId,
creatorName: creatorName,
messageTrackingId: messageTrackingId
};
this.http
.post<{ message: string; messagingId: string; creator: string }>(
"http://localhost:3000/api/messages",
messaging
)
.subscribe(responseData => {
console.log(responseData);
const id = responseData.messagingId;
messaging.id = id;
console.log("Message sent successfully!");
// window.location.reload();
// this.posts.push();
// this.postsUpdated.next([...this.posts]);
});
}
replyToMessage(
recipient: string,
message: string,
creationDate: Date,
recipientId: string,
creatorName: string,
messageTrackingId: string
) {
const messaging: Messages = {
id: null,
recipient: recipient,
message: message,
creationDate: creationDate,
creator: null,
recipientId: recipientId,
creatorName: creatorName,
messageTrackingId: messageTrackingId
};
this.http
.post<{ message: string; messagingId: string; creator: string }>(
"http://localhost:3000/api/messages",
messaging
)
.subscribe(responseData => {
console.log(responseData);
const id = responseData.messagingId;
messaging.id = id;
console.log("Message sent successfully!");
});
}
getMessages(recipientId: string) {
return this.http
.get<{
message: string;
posts: any;
maxPosts: number;
messageList: string;
}>("http://localhost:3000/api/messages?recipientId=" + recipientId)
.pipe(
map(retrievedData => {
return {
posts: retrievedData.posts.map(post => {
return {
creator: post.creator,
recipientId: post.recipientId,
creationDate: post.creationDate,
messageTrackingId: post.messageTrackingId,
creatorName: post.creatorName,
id: post._id
};
}),
maxPosts: retrievedData.maxPosts
};
})
);
}
以下是收件人回复邮件的示例,因此发件人可以使用messageTrackingId
先发送消息,然后回复消息。由于收件人已回复,因此发件人具有messageTrackingId,可用于向同一用户发送下一封邮件。
Made It
{ _id: 5e0674ddd55aae5294370870,
creator: 5df0014e25ee451beccf588a,
recipient: 'joe',
recipientId: '5df00d08c713f722909c99c1',
message: 'This is the initial message',
messageTrackingId: '3cb3f5bb-5e17-49a7-8aca-4a61ddd1d847',
creatorName: 'andy',
creationDate: 2019-12-27T21:17:17.155Z,
__v: 0 }
Made It
{ _id: 5e067529d55aae5294370872,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is the reply message',
messageTrackingId: '3cb3f5bb-5e17-49a7-8aca-4a61ddd1d847',
creatorName: 'joe',
creationDate: 2019-12-27T21:18:33.947Z,
__v: 0 }
如果收件人从不答复,而发件人发送另一条消息,则会发生这种情况:
Made It
{ _id: 5e06756bd55aae5294370873,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is the first message',
messageTrackingId: '2077a8e6-844c-4639-a4fa-7aee0b8beaf4',
creatorName: 'joe',
creationDate: 2019-12-27T21:19:39.217Z,
__v: 0 }
Made It
{ _id: 5e06757cd55aae5294370874,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is another message to same user.',
messageTrackingId: 'feeb0e20-432e-4c9a-9f59-45913c194edc',
creatorName: 'joe',
creationDate: 2019-12-27T21:19:56.257Z,
__v: 0 }
答案 0 :(得分:1)
您可以使用以下汇总来确保每对(recipient
,sender
)对仅返回一个文档:
db.Messages.aggregate([
{
$addFields: { conversants: [ "$recipientId", "$creator" ] }
},
{
$match: { conversants: req.query.recipientId }
},
{
$addFields: { conversant: { $arrayElemAt: [ { $filter: { input: "$conversants", cond: { $ne: [ "$$this", "5df0014e25ee451beccf588a" ] } } } , 0 ] } }
},
{
$sort: { creationDate: 1 }
},
{
$group: {
_id: "$conversant",
message: { $first: "$message" },
recipientId: { $first: "$recipientId" },
creator: { $first: "$creator" },
messageTrackingId: { $first: "$messageTrackingId" },
creationDate: { $first: "$creationDate" }
}
},
{
$lookup: {
from: "users",
let: { creator: "$creator" },
pipeline: [
{ $match: { $expr: { $eq: [ "$_id", "$$creator" ] } } },
{ $project: { creatorName: 1 } }
],
as: "creatorName"
}
},
{
$addFields: { creatorName: { $arrayElemAt: [ "$creatorName", 0 ] } }
}
])
这里的想法是您创建代表两个ID的附加字段:creator
和recipient
。这将使您能够做两件事:使用recipientId
(可能也是发送者)进行过滤-第二步,并选择conversant
值始终是第二个人-无论是否在您的请求在该对话中发送或接收了一条消息。然后,您可以在该字段上$group
以确保每次“会话”中仅收到一条消息。不必将$addToSet
与$arrayElemAt
一起使用,您只需运行$first
。相对较重的$lookup
也可以作为最后一步运行,因为您需要每个“对话者”一次获取该数据。
编辑:前三个阶段可以替换为以下阶段-由于将尽快应用过滤,因此这将大大提高性能:
{
$match: {
$or: [ { recipientId: req.query.recipientId }, { creator: req.query.recipientId } ]
}
},
{
$addFields: {
conversant: {
$cond: [ { $ne: [ "$recipientId", req.query.recipientId ] }, "$recipientId", "$creator" ]
}
}
},