MongoDB:使用多个$ cond的$ sum

发布于 2025-02-09 06:33:50 字数 1373 浏览 1 评论 0原文

我希望计算基于数组元素的大小。 我可以使用多个$ cond的$ sum?

类似的内容:

    {
    $project: {
      points: {
        $sum: [
          {
            $cond: [
              {
                $and: [{ $gt: [{ $arrayElemAt: ["$matches", 0] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 1] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 2] }, 0] }],
              },
              10,
              0,
            ],
          },
          {
            $cond: [
              {
                $and: [{ $gt: [{ $arrayElemAt: ["$matches", 3] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 4] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 5] }, 0] }],
              },
              10,
              0,
            ],
          },
        ],
      },
    },
  },

此脚本总是返回20,即使并非所有条件都是

文档的真实示例:

{ 
"_id" : NumberInt(5), 
"total_matches" : NumberInt(0), 
"matches" : [
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ]
]

}

输出

{ 
    "_id" : NumberInt(5), 
    "points" : 20.0, 
    "matches" : [
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ]
    ]
}

I want calculate points based to the size of an array element.
I can use $sum with multiple $cond?

Something like this:

    {
    $project: {
      points: {
        $sum: [
          {
            $cond: [
              {
                $and: [{ $gt: [{ $arrayElemAt: ["$matches", 0] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 1] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 2] }, 0] }],
              },
              10,
              0,
            ],
          },
          {
            $cond: [
              {
                $and: [{ $gt: [{ $arrayElemAt: ["$matches", 3] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 4] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 5] }, 0] }],
              },
              10,
              0,
            ],
          },
        ],
      },
    },
  },

This script always returns 20 even if not all conditions are true

Example of document:

{ 
"_id" : NumberInt(5), 
"total_matches" : NumberInt(0), 
"matches" : [
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ], 
    [

    ]
]

}

OUTPUT

{ 
    "_id" : NumberInt(5), 
    "points" : 20.0, 
    "matches" : [
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ], 
        [

        ]
    ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2025-02-16 06:33:50

好的,问题是我在查询中忘记了$尺寸。
正确的查询是:

 {
    $project: {
      points: {
        $sum: [
          {
            $cond: [
              {
                $and: [{ $gt: [{ $size:{ $arrayElemAt: ["$matches", 0] }}, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 1] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 2] }, 0] }],
              },
              10,
              0,
            ],
          },
          {
            $cond: [
              {
                $and: [{ $gt: [{ $size:{ $arrayElemAt: ["$matches", 3] }}, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 4] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 5] }, 0] }],
              },
              10,
              0,
            ],
          },
        ],
      },
    },
  }

Ok, the problem is that I forgot $size in the query.
The right query is this:

 {
    $project: {
      points: {
        $sum: [
          {
            $cond: [
              {
                $and: [{ $gt: [{ $size:{ $arrayElemAt: ["$matches", 0] }}, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 1] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 2] }, 0] }],
              },
              10,
              0,
            ],
          },
          {
            $cond: [
              {
                $and: [{ $gt: [{ $size:{ $arrayElemAt: ["$matches", 3] }}, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 4] }, 0] }, { $gt: [{ $arrayElemAt: ["$matches", 5] }, 0] }],
              },
              10,
              0,
            ],
          },
        ],
      },
    },
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文