只有在使用golang实现查询时,使用聚合查询中的空白数据



我试图使用聚合获得文档数组。聚合以同时匹配聊天数组,按时间戳排序,然后按chat_id分组,仅从每个消息中选择最后一条消息。因此,我有一个包含三个chat_id的数组,我想获得具有该chat_id的最后三条消息。

下面是两个例子第一个是控制台中的聚合,以及我得到的响应示例。第二个是golang的变体,但我在输出

中得到空对象这是MongoDB的游乐场工作的例子

第一个例子

db.message.aggregate([
{
$match: {
chat_id: {
$in: ["dtlzoD9fI15q8_YM6eqp", "unps_ZwBZ7mCubC3TsKl"],
},
}
},
{
$sort: {
"created": -1,
},
},
{
$group: {
"_id": {"chat_id": "$chat_id"},
"doc": { "$last": "$$ROOT" }
}
}
])

第一反应

[
{
"_id": {
"chat_id": "unps_ZwBZ7mCubC3TsKl"
},
"doc": {
"_id": {"$oid": "63ac61d71a11d3b05340c001"},
"id": "wsBFKoN51q5v4Nnv3A-B",
"chat_id": "unps_ZwBZ7mCubC3TsKl",
"body": "Certainty determine at of arranging perceived situation or. Or wholly pretty county in oppose. Favour met itself wanted settle put garret twenty. In astonished apartments resolution so an it. Unsatiable on by contrasted to reasonable companions an. On otherwise no admitting to suspicion furniture it. ",
"created": 1672241623466
}
},
{
"_id": {
"chat_id": "dtlzoD9fI15q8_YM6eqp"
},
"doc": {
"_id": {"$oid": "63ac607f141f0526cba7113d"},
"id": "jhjVxQUzQbPCLebnupS9",
"chat_id": "dtlzoD9fI15q8_YM6eqp",
"body": "Certainty determine at of arranging perceived situation or. Or wholly pretty county in oppose. Favour met itself wanted settle put garret twenty. In astonished apartments resolution so an it. Unsatiable on by contrasted to reasonable companions an. On otherwise no admitting to suspicion furniture it. ",
"created": 1672241279354
}
}
]

第二个例子。这不是工作示例

type Message struct {
ID      string `json:"id" bson:"id"`
ChatID  string `json:"chat_id" bson:"chat_id"`
Body    string `json:"body" bson:"body"`
Created int64  `json:"created" bson:"created"`
}
...
ids := []string{"unps_ZwBZ7mCubC3TsKl", "dtlzoD9fI15q8_YM6eqp"}
matchStage := bson.D{{
Key: "$match", Value: bson.D{{Key: "chat_id", Value: bson.D{{Key: "$in", Value: ids}}}},
}}
sortStage := bson.D{{"$sort", bson.D{{"created", -1}}}}
groupStage := bson.D{{
Key: "$group", Value: bson.D{
{
Key: "_id", Value: bson.D{
{"chat_id", "$chat_id"},
},
},
{
Key: "message", Value: bson.D{
{"$last", "$$ROOT"},
},
},
},
}}
cursor, err := db.Aggregate(context.Background(), mongo.Pipeline{matchStage, groupStage, sortStage})
if err != nil {}
var messages []*Message
err = cursor.All(context.Background(), &messages)
if err != nil {
fmt.Print("Cursor err: ", err)
return
}
defer func() {
err := cursor.Close(context.Background())
if err != nil {
return
}
}()
<<p>第二反应/strong>
MSG: &{   0} // empty data
MSG: &{   0}

我想从聚合中得到什么

[
{
"_id": {"$oid": "63ac607f141f0526cba7113d"},
"id": "jhjVxQUzQbPCLebnupS9",
"chat_id": "dtlzoD9fI15q8_YM6eqp",
"body": "Certainty determine at of arranging perceived situation or. Or wholly pretty county in oppose. Favour met itself wanted settle put garret twenty. In astonished apartments resolution so an it. Unsatiable on by contrasted to reasonable companions an. On otherwise no admitting to suspicion furniture it. ",
"created": 1672241279354
},
{
"_id": {"$oid": "63ac61d71a11d3b05340c001"},
"id": "wsBFKoN51q5v4Nnv3A-B",
"chat_id": "unps_ZwBZ7mCubC3TsKl",
"body": "Certainty determine at of arranging perceived situation or. Or wholly pretty county in oppose. Favour met itself wanted settle put garret twenty. In astonished apartments resolution so an it. Unsatiable on by contrasted to reasonable companions an. On otherwise no admitting to suspicion furniture it. ",
"created": 1672241623466
},
]

这个问题可能是因为你的消息模式不正确(注意你的JS聚合和你的go之间有一个小的区别,在前者使用doc,在后者使用message,我将坚持使用message

type MessageInner struct {
ID      string `json:"id" bson:"id"`
ChatID  string `json:"chat_id" bson:"chat_id"`
Body    string `json:"body" bson:"body"`
Created int64  `json:"created" bson:"created"`
}
type Message struct {
MessageInner Doc `json:"message" bson:"message"`
}

另一种选择是在末尾使用$replaceRoot聚合阶段

{ $replaceRoot: { newRoot: "$message" } }

最新更新