聚合,将一个集合中的数据插入到另一个集合中

2024年 2月 11日 43.2k 0

聚合,将一个集合中的数据插入到另一个集合中

php小编柚子在这篇文章中将为大家介绍一个常见的操作——聚合,即将一个集合中的数据插入到另一个集合中。聚合操作在编程中非常常见,可以用于合并数据、去重、筛选等多种场景。通过聚合操作,我们可以轻松地对数据进行处理和管理,提高代码的效率和可读性。接下来,我们将详细介绍聚合操作的使用方法及注意事项,帮助大家更好地掌握这一技巧。

问题内容

我正在尝试执行以下操作,从特定用户的 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 游乐场

这里是一个旧 mongo playground 与 $merge 进行更新到一个集合。

以上就是聚合,将一个集合中的数据插入到另一个集合中的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

如何删除WordPress中的所有评论
检查WordPress服务器磁盘使用情况的7种简便方法(查找大文件和数据)
如何更改WordPress常量FS_METHOD
如何为区块编辑器、Elementor等构建WordPress文章模板
如何彻底地删除WordPress主题及相关内容
如何使用WordPress搭建一个内网

发布评论