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

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.。我不认为这是一个语法错误。请帮助我,非常感谢


慕尼黑5688855
浏览 217回答 1
1回答

牧羊人nacy

数据方面的每个管道都应该是数组的元素    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        }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go