当使用`$facet`操作符时,Golang和MongoDB报错。

huangapple go评论86阅读模式
英文:

Golang, MongoDB alert error when using `$facet` operator

问题

我将你的代码翻译成中文如下:

metadataStage := bson.D{
    {"$facet", bson.D{
        {"metadata", bson.A{bson.D{
            {"$group", bson.D{
                {"_id", nil},
                {"total", bson.D{{"$sum", 1}}}}}}, // 获取满足以上所有条件的文档数量
        }},
        {"data", bson.A{bson.D{
            //{"$sort", sortStage}, // 按字段和方向排序
            {"$skip", skip}, // 跳过指定数量的文档
            {"$limit", limit}}}}, // 限制文档数量
    }},
}

但是当我运行代码时,Mongo驱动程序总是报错A pipeline stage specification object must contain exactly one field.。我不认为这是语法错误。请帮助我,非常感谢。

英文:

I set up my facet like that:

	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
		}},
	}

but when I run my code, Mongo Driver always alerts the error A pipeline stage specification object must contain exactly one field.. I do not think that this is a syntax error. Please help me, thank you so much

答案1

得分: 1

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

metadataStage := bson.D{
    {"$facet", bson.D{
        {"metadata", bson.A{
            bson.D{{"$group", bson.D{
                {"_id", nil},
                {"total", bson.D{{"$sum", 1}}}}}}, // 获取符合上述条件的文档数量
        }},
        {"data", bson.A{
            //bson.D{{"$sort", sortStage}}, // 按字段和方向排序
            bson.D{{"$skip", skip}}, // 跳过指定数量的文档
            bson.D{{"$limit", limit}}}}}, // 限制文档数量
    }}
}

或者,你可以明确声明管道的阶段以便清晰理解。

metadataStage := bson.D{
    {"$facet", bson.D{
        {"metadata", mongo.Pipeline{
            bson.D{{"$group", bson.D{
                {"_id", nil},
                {"total", bson.D{{"$sum", 1}}}}}}, // 获取符合上述条件的文档数量
        }},
        {"data", mongo.Pipeline{
            //bson.D{{"$sort", sortStage}}, // 按字段和方向排序
            bson.D{{"$skip", skip}}, // 跳过指定数量的文档
            bson.D{{"$limit", limit}}}}}, // 限制文档数量
    }}
}
英文:

Each pipeline for the data facet should be element of an array

	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
		}}

Alternately, you could write to be clear that you are declaring stages of the pipeline for the facets.

	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
		}}

huangapple
  • 本文由 发表于 2022年10月2日 00:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73919941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定