
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"`
...
}
第一种方法:我需要此方法来获取特定聊天参与者的活动聊天列表。
PhpEIP企业信息化平台主要解决企业各类信息的集成,能把各种应用系统(如内容管理系统,网上商城,论坛系统等)统一到企业信息化平台中,整个系统采用简单易用的模板引擎,可自定义XML标签,系统采用开放式模块开发,符合开发接口的模块可完全嵌入到平台;内容管理模块可自定义内容模型,系统自带普通文章模型和图片集模型,用户可以定义丰富的栏目构建企业门户,全站可生成静态页面,提供良好的搜索引擎优化;会员管理模
0
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 进行更新到一个集合。
以上就是聚合,将一个集合中的数据插入到另一个集合中的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号