gpt4 book ai didi

mongodb - MongoDB管道中的多个顶级聚合查询

转载 作者:行者123 更新时间:2023-12-01 21:09:33 24 4
gpt4 key购买 nike

我有以下查询:

match := bson.D{{"$match", bson.D{{"venue", venueID}}}}
group := bson.D{{"$lookup", bson.M{
"from": "labels",
"localField": "label_ids",
"foreignField": "_id",
"as": "labels",
}}, {"$graphLookup", bson.M{
"from": "menus",
"startWith": "$child_ids",
"connectFromField": "child_ids",
"connectToField": "_id",
"as": "children",
"maxDepth": 5,
"depthField": "level",
}}}

cur, err := m.collection.Aggregate(ctx, mongo.Pipeline{group, match})
我有两个相关的字段,其中一个是图形结构(菜单),每个父元素都有一个ID数组,用于每个子元素。
第二个字段,标签,只是一对多的查询。标签和菜单应该是可重用的,因此不能嵌入单个父实体中。上面概述的查询对我来说很有意义,但是出现以下错误: A pipeline stage specification object must contain exactly one field.谢谢!

最佳答案

MongoDB管道中的每个元素都必须是单个阶段,例如$match$group
您的group元素包含两个阶段:$lookup$graphLookup
拆分它们并单独列出:

match := bson.D{{"$match", bson.D{{"venue", venueID}}}}
group := bson.D{{"$lookup", bson.M{
"from": "labels",
"localField": "label_ids",
"foreignField": "_id",
"as": "labels",
}}}
graphLookup := bson.D{{"$graphLookup", bson.M{
"from": "menus",
"startWith": "$child_ids",
"connectFromField": "child_ids",
"connectToField": "_id",
"as": "children",
"maxDepth": 5,
"depthField": "level",
}}}

cur, err := m.collection.Aggregate(ctx, mongo.Pipeline{group, graphLookup, match})

关于mongodb - MongoDB管道中的多个顶级聚合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63282222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com