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

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

Golang: MongoDB - count length of nested arrays in Projection

问题

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

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

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

{
   "test":"other",
   "list":[
      {
        "number":[
            {
                "value": "1"
            }, 
            {
                "value": "2"
            },
            {
                "value": "2"
            }
        ]
      },
      {
        "number":[
            {
                "value": "1"
            }, 
            {
                "value": "2"
            },
            {
                "value": "2"
            }
        ]
      },
      {
        "number":[
            {
                "value": "1"
            }, 
            {
                "value": "2"
            }
        ]
      }
   ]
}

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

英文:

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

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

All objects under number are to be counted

{
   "test":"other",
   "list":[
      {
		"number":[
			{
				"value": "1"
			}, 
			{
				"value": "2"
			},
			{
				"value": "2"
			}
		]
      },
      {
		"number":[
			{
				"value": "1"
			}, 
			{
				"value": "2"
			},
			{
				"value": "2"
			}
		]
      },
      {
		"number":[
			{
				"value": "1"
			}, 
			{
				"value": "2"
			}
		]
      }
   ]
}

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

答案1

得分: 1

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

db.collection.aggregate([
  {$project: {
      test: "$test",
      count: {
        $reduce: {
          input: "$list",
          initialValue: 0,
          in: {$add: ["$$value", {$size: "$$this.number"}]}
        }
      }
    }
  }
])

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

英文:

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

db.collection.aggregate([
  {$project: {
      test: "$test",
      count: {
        $reduce: {
          input: "$list",
          initialValue: 0,
          in: {$add: ["$$value", {$size: "$$this.number"}]}
        }
      }
    }
  }
])

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:

确定