为了账号安全,请及时绑定邮箱和手机立即绑定

Golang,MongoDB 在使用 `$facet` 运算符时警告错误

Golang,MongoDB 在使用 `$facet` 运算符时警告错误

Go
慕尼黑5688855 2023-02-21 16:30:52
facet我这样设置:    metadataStage := bson.D{        {"$facet", bson.D{            {"metadata", bson.A{bson.D{                {"$group", bson.D{                    {"_id", nil},                    {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions            }},            {"data", bson.A{bson.D{                //{"$sort", sortStage}, // sort by field and direction                {"$skip", skip}, // skip number of documents                {"$limit", limit}}}}, // limit them        }},    }但是当我运行我的代码时,Mongo Driver 总是会提示错误A pipeline stage specification object must contain exactly one field.。我不认为这是一个语法错误。请帮助我,非常感谢
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

数据方面的每个管道都应该是数组的元素


    metadataStage := bson.D{

        {"$facet", bson.D{

            {"metadata", bson.A{

                bson.D{{"$group", bson.D{

                    {"_id", nil},

                    {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions

            }},

            {"data", bson.A{

                //bson.D{{"$sort", sortStage}}, // sort by field and direction

                bson.D{{"$skip", skip}}, // skip number of documents

                bson.D{{"$limit", limit}}}}}, // limit them

        }}

或者,您可以写清楚您正在为方面声明管道的阶段。


    metadataStage := bson.D{

        {"$facet", bson.D{

            {"metadata", mongo.Pipeline{

                bson.D{{"$group", bson.D{

                    {"_id", nil},

                    {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions

            }},

            {"data", mongo.Pipeline{

                //bson.D{{"$sort", sortStage}}, // sort by field and direction

                bson.D{{"$skip", skip}},      // skip number of documents

                bson.D{{"$limit", limit}}}}}, // limit them

        }}


查看完整回答
反对 回复 2023-02-21
  • 1 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信