mongodb-使用条件的$ addtoset

发布于 2025-02-06 05:16:39 字数 589 浏览 3 评论 0原文

在小组聚合中,我只是尝试将名称添加到20岁的情况下:

假设我有以下两个文档:

_id: ...
timestamp: ...
name: Max
age: 20


_id: ...
timestamp: ...
name: Brian
age: 21

现在,我正在尝试对这些文档进行分组,并在$ $ group 我正在尝试将所有20岁儿童的名称添加到一个集合中:

{
  $group: {
    _id: {
      bins: {
        $dateTrunc: {
          date: '$timestamp',
          unit: 'week',
          binSize: 1
        }
      }
    },
    'NamesWithAge20': {
      $addToSet: '$name'
    }
  }
}

我只对'namesWithage20'set感兴趣。在此代码中,现在将Max和Brian都添加到集合中,但是我想引入一个条件,该条件仅在年龄20岁时添加名称。在这个阶段,这是可能的吗?

In a group aggregation, I'm simply trying to add names to a set if the age is 20:

Let's say I have the following two documents:

_id: ...
timestamp: ...
name: Max
age: 20


_id: ...
timestamp: ...
name: Brian
age: 21

Now I'm trying to group these and in the $group and I'm trying to add the name of all 20 year-olds to a set:

{
  $group: {
    _id: {
      bins: {
        $dateTrunc: {
          date: '$timestamp',
          unit: 'week',
          binSize: 1
        }
      }
    },
    'NamesWithAge20': {
      $addToSet: '$name'
    }
  }
}

I'm only interested in the 'NamesWithAge20' set. In this code right now both Max and Brian are added to the set, but I want to introduce a condition that only adds the name if the age is 20. Is that possible in this stage?

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

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

发布评论

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

评论(1

愛上了 2025-02-13 05:16:39

是的,您可以,使用$ cond $$ remove

$$删除

一个评估缺失值的变量。允许有条件排除字段。在一个$项目中,将一个设置为变量删除的字段排除在输出中。

db.collection.aggregate([
  {
    $group: {
      _id: {
        bins: {
          $dateTrunc: {
            date: "$timestamp",
            unit: "week",
            binSize: 1
          }
        }
      },
      "NamesWithAge20": {
        $addToSet: {
          $cond: {
            if: {
              $eq: [
                "$age",
                20
              ]
            },
            then: "$name",
            else: "$REMOVE"
          }
        }
      }
    }
  }
])

示例mongo playground


如果您只想用age>年龄查询文档:20,,建议添加$ MATD阶段以在$ group阶段之前过滤文档。

db.collection.aggregate([
  {
    $match: {
      "age": 20
    }
  },
  {
    $group: {
      _id: {
        bins: {
          $dateTrunc: {
            date: "$timestamp",
            unit: "week",
            binSize: 1
          }
        }
      },
      "NamesWithAge20": {
        $addToSet: "$name"
      }
    }
  }
])

示例mongo playground($ filter

Yes you can, work with $cond and with $$REMOVE.

$$REMOVE

A variable which evaluates to the missing value. Allows for the conditional exclusion of fields. In a $project, a field set to the variable REMOVE is excluded from the output.

db.collection.aggregate([
  {
    $group: {
      _id: {
        bins: {
          $dateTrunc: {
            date: "$timestamp",
            unit: "week",
            binSize: 1
          }
        }
      },
      "NamesWithAge20": {
        $addToSet: {
          $cond: {
            if: {
              $eq: [
                "$age",
                20
              ]
            },
            then: "$name",
            else: "$REMOVE"
          }
        }
      }
    }
  }
])

Sample Mongo Playground


If you just want to query the document with age: 20, would suggest adding $match stage to filter the documents before $group stage.

db.collection.aggregate([
  {
    $match: {
      "age": 20
    }
  },
  {
    $group: {
      _id: {
        bins: {
          $dateTrunc: {
            date: "$timestamp",
            unit: "week",
            binSize: 1
          }
        }
      },
      "NamesWithAge20": {
        $addToSet: "$name"
      }
    }
  }
])

Sample Mongo Playground ($filter)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文