Golang: MongoDB – 在投影中计算嵌套数组的长度

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

Golang: MongoDB - count length of nested arrays in Projection

问题

在MongoDB中,我喜欢将所有嵌套数组的长度加在一起进行计数。
这只是一个MongoDB文档。

  1. idForDB := "621101966rf42c24a8f41b87"
  2. ctx, cancel := context.WithTimeout(context.Background(), time.Second * 20)
  3. defer cancel()
  4. options := options.FindOneOptions{
  5. Projection: bson.M{
  6. "test": "$test",
  7. "count": bson.M{}, // count应该放在这里
  8. },
  9. }
  10. result := clients.MongoClient.Database("somedb").Collection("somecollection").FindOne(ctx, bson.M{"_id": idForDB}, &options)

所有在"number"下的对象都要被计数

  1. {
  2. "test":"other",
  3. "list":[
  4. {
  5. "number":[
  6. {
  7. "value": "1"
  8. },
  9. {
  10. "value": "2"
  11. },
  12. {
  13. "value": "2"
  14. }
  15. ]
  16. },
  17. {
  18. "number":[
  19. {
  20. "value": "1"
  21. },
  22. {
  23. "value": "2"
  24. },
  25. {
  26. "value": "2"
  27. }
  28. ]
  29. },
  30. {
  31. "number":[
  32. {
  33. "value": "1"
  34. },
  35. {
  36. "value": "2"
  37. }
  38. ]
  39. }
  40. ]
  41. }

"Number"字段的输出应该是8,因为在"number"对象下有8个文档。

英文:

In MongoDB i like to count all length of nested arrays togehter.
It is just one mongoDB document.

  1. idForDB := "621101966rf42c24a8f41b87"
  2. ctx, cancel := context.WithTimeout(context.Background(), time.Second * 20)
  3. defer cancel()
  4. options := options.FindOneOptions{
  5. Projection: bson.M{
  6. "test": "$test",
  7. "count": bson.M{}, // count should placed here
  8. },
  9. }
  10. result := clients.MongoClient.Database("somedb").Collection("somecollection").FindOne(ctx, bson.M{"_id": idForDB}, &options)

All objects under number are to be counted

  1. {
  2. "test":"other",
  3. "list":[
  4. {
  5. "number":[
  6. {
  7. "value": "1"
  8. },
  9. {
  10. "value": "2"
  11. },
  12. {
  13. "value": "2"
  14. }
  15. ]
  16. },
  17. {
  18. "number":[
  19. {
  20. "value": "1"
  21. },
  22. {
  23. "value": "2"
  24. },
  25. {
  26. "value": "2"
  27. }
  28. ]
  29. },
  30. {
  31. "number":[
  32. {
  33. "value": "1"
  34. },
  35. {
  36. "value": "2"
  37. }
  38. ]
  39. }
  40. ]
  41. }

The output for the "Number" field should be 8 because there are 8 documents under the "number" object.

答案1

得分: 1

使用MongoDB语法,你可以使用$reduce来实现:

  1. db.collection.aggregate([
  2. {$project: {
  3. test: "$test",
  4. count: {
  5. $reduce: {
  6. input: "$list",
  7. initialValue: 0,
  8. in: {$add: ["$$value", {$size: "$$this.number"}]}
  9. }
  10. }
  11. }
  12. }
  13. ])

playground example上查看它的工作原理。

英文:

Using mongoDB syntax, you can use $reduce for it:

  1. db.collection.aggregate([
  2. {$project: {
  3. test: "$test",
  4. count: {
  5. $reduce: {
  6. input: "$list",
  7. initialValue: 0,
  8. in: {$add: ["$$value", {$size: "$$this.number"}]}
  9. }
  10. }
  11. }
  12. }
  13. ])

See how it works on the playground example

huangapple
  • 本文由 发表于 2022年7月11日 22:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/72940060.html
匿名

发表评论

匿名网友

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

确定