聚合,将一个集合中的数据插入到另一个集合中
来源:stackoverflow
时间:2024-04-13 20:21:32 278浏览 收藏
怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《聚合,将一个集合中的数据插入到另一个集合中》,涉及到,有需要的可以收藏一下
我正在尝试执行以下操作,从特定用户的 chat 集合中获取聊天列表,并将发送的 message 集合中的最后一条消息添加到每个聊天的该列表中。
现在这是如何工作的,我有两种方法,如下所述
首先,我只是使用聊天成员 id 获取聊天列表,第二种方法使用聚合来查找每个聊天的最后一条消息,然后我只需将消息与聊天 id 进行匹配
收藏聊天:
type chat struct {
id string `json:"id" bson:"id"`
participants []string `json:"participants" bson:"participants"`
lastmessage *message `json:"last_message,omitempty" bson:"last_message"`
...
}
附注
lastmessage - 始终为零,我只需要它来为用户编写响应。
集合message:
type message struct {
id string `json:"id" bson:"id"`
chatid string `json:"chat_id" bson:"chat_id"`
fromid string `json:"from_id" bson:"from_id"`
createdate int64 `json:"create_date" bson:"create_date"`
body string `json:"body" bson:"body"`
updateat int64 `json:"update_at" bson:"update_at"`
...
}
第一种方法:我需要此方法来获取特定聊天参与者的活动聊天列表。
func activechats(ctx context.context, uid string) ([]*chat, error) {
...
filter := bson.d{primitive.e{key: "participants", value: uid}}
cursor, err := r.col.find(ctx, filter, nil)
if err != nil {...}
var ch []*chat
if err = cursor.all(ctx, &ch); err != nil {...}
if err = cursor.close(ctx); err != nil {...}
...
}
第二种方法:我需要此方法来获取每个聊天的最后一条消息,输入是一组聊天 id,并且对于每个聊天,我都会搜索最后一条消息(如果有)。为此,我使用聚合。
func lastmessages(ctx context.context, chatids []string) (map[string]*message, error) {
matchstage := bson.d{
primitive.e{
key: "$match", value: bson.d{
primitive.e{
key: "chat_id", value: bson.d{
primitive.e{key: "$in", value: chatids},
},
},
},
}}
sortstage := bson.d{primitive.e{key: "$sort", value: bson.d{primitive.e{key: "created", value: -1}}}}
groupstage := bson.d{primitive.e{
key: "$group", value: bson.d{
primitive.e{
key: "_id", value: bson.d{
primitive.e{key: "chat_id", value: "$chat_id"},
},
},
primitive.e{
key: "message", value: bson.d{
primitive.e{key: "$first", value: "$$root"},
},
},
},
}}
cursor, err := r.colmessage.aggregate(ctx, mongo.pipeline{matchstage, groupstage, sortstage})
if err != nil {...}
var res []*aggregationresultgenerated
if err = cursor.all(ctx, &res); err != nil {...}
...
}
我知道这是一个非常糟糕的解决方案,但这是我迄今为止所能想到的,非常遗憾(不工作)。我尝试解决这个问题
db.chat.aggregate([
{
$match: {
participants: "participant_id",
},
{
$lookup: {
from: "message", // other table name
localfield: "id", // name of chat table field
foreignfield: "chat_id", // name of message table field
as: "msg",
}
},
{
$unwind: "$msg",
},
{
$match: {
chat_id : {
$in: ["$$root._id"],
},
},
},
{
$sort: {
"created": -1,
},
},
{
$group: {
"_id": {
"chat_id": "$chat_id"
},
"doc": {
"$last": "$$root"
}
}
},
{
$project: {
last_message: "$msg",
}
}
])
我的问题是:如何使用聚合来获取特定用户的聊天列表,并为每个聊天从 message 集合中添加对象 chat 中字段 last_message 中的最后一条消息?
现在如何运作:
{
"chats": [
{
"id": "4hWsHam3ZZpoyIw44q3D",
"title": "Chat example",
"create-date": 1674476855918,
"participants": [
"63ce54460aeee5e72c778d90",
"63ce54460aeee5e72c778d92"
],
"owner_id": "63ce54460aeee5e72c778d90",
"last_message": {
"id": "tzwekCiCLSXJ4tfdQuHH",
"chat_id": "4hWsHam3ZZpoyIw44q3D",
"from_id": "63ce54460aeee5e72c778d92",
"create_date": 1674557062031,
"body": "text",
"update_at": 0,
"viewed": false
},
"unread": 5
},
{
"id": "Anjrr9RCWFzq030Cwz7S",
"title": "New chat One",
"create-date": 1674476909054,
"participants": [
"63ce54460aeee5e72c778d90",
"63ce54460aeee5e72c778d96"
],
"owner_id": "63ce54460aeee5e72c778d90",
"last_message": {
"id": "7YqhhS1-EfMRSZtGCH0Z",
"chat_id": "Anjrr9RCWFzq030Cwz7S",
"from_id": "63ce54460aeee5e72c778d96",
"create_date": 1674575017115,
"body": "text",
"update_at": 0,
},
"unread": 1
},
]
}
正确答案
编辑:正如op在评论中提到的,update/$merge到集合是没有必要的。
您可以简单地在 $lookup 的子管道中执行 $sort + $limit 方法。执行 $unwind 将查找结果写入 last_message 字段。最后,执行 $merge 以更新回 chat 集合。
db.chat.aggregate([
{
$match: {
participants: "63ce54460aeee5e72c778d90",
}
},
{
$lookup: {
from: "message",
localField: "id",
foreignField: "chat_id",
pipeline: [
{
$sort: {
created: -1
}
},
{
$limit: 1
}
],
as: "last_message",
}
},
{
$unwind: {
path: "$last_message",
preserveNullAndEmptyArrays: true
}
},
{
$project: {
last_message: "$last_message"
}
}
])
这里是一个旧 mongo playground 与 $merge 进行更新到一个集合。
以上就是《聚合,将一个集合中的数据插入到另一个集合中》的详细内容,更多关于的资料请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习