英文:
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
		}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论