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

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

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

问题

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

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

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

英文:

I set up my facet like that:

  1. metadataStage := bson.D{
  2. {"$facet", bson.D{
  3. {"metadata", bson.A{bson.D{
  4. {"$group", bson.D{
  5. {"_id", nil},
  6. {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions
  7. }},
  8. {"data", bson.A{bson.D{
  9. //{"$sort", sortStage}, // sort by field and direction
  10. {"$skip", skip}, // skip number of documents
  11. {"$limit", limit}}}}, // limit them
  12. }},
  13. }

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

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

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

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

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

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

  1. metadataStage := bson.D{
  2. {"$facet", bson.D{
  3. {"metadata", bson.A{
  4. bson.D{{"$group", bson.D{
  5. {"_id", nil},
  6. {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions
  7. }},
  8. {"data", bson.A{
  9. //bson.D{{"$sort", sortStage}}, // sort by field and direction
  10. bson.D{{"$skip", skip}}, // skip number of documents
  11. bson.D{{"$limit", limit}}}}}, // limit them
  12. }}

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

  1. metadataStage := bson.D{
  2. {"$facet", bson.D{
  3. {"metadata", mongo.Pipeline{
  4. bson.D{{"$group", bson.D{
  5. {"_id", nil},
  6. {"total", bson.D{{"$sum", 1}}}}}}, // get number of documents that matches the all above conditions
  7. }},
  8. {"data", mongo.Pipeline{
  9. //bson.D{{"$sort", sortStage}}, // sort by field and direction
  10. bson.D{{"$skip", skip}}, // skip number of documents
  11. bson.D{{"$limit", limit}}}}}, // limit them
  12. }}

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:

确定