【问题标题】:MongoDB Aggregation $size on Nested Field嵌套字段上的 MongoDB 聚合 $size
【发布时间】:2018-09-21 01:15:52
【问题描述】:

我正在尝试执行一个棘手的聚合以返回集合中文档内嵌套数组的大小。

这里是如何重新创建我的示例数据:

db.test.insert({
    projects: [
        {
            _id: 1,
            comments: [
                'a',
                'b',
                'c'
            ]
        },
        {
            _id: 2,
            comments: [
                'a',
                'b'
            ]
        },
        {
            _id: 3,
            comments: []
        }
    ]
})

我要执行的聚合在这里:

db.test.aggregate([
    // enter aggregation here
])

这是所需的输出:

[{
    projects: [
        {
            _id: 1,
            comment_count: 3
        },
        {
            _id: 2,
            comment_count: 2
        },
        {
            _id: 3,
            comment_count: 0
        }
    ]
}]

我正在为如何写这个而苦苦挣扎。如果我尝试以下操作:

"projects.comment_count": {"$size": }

结果返回结果数组的大小:

[{
    projects: [
        {
            _id: 1,
            comment_count: 3
        },
        {
            _id: 2,
            comment_count: 3
        },
        {
            _id: 3,
            comment_count: 3
        }
    ]
}]

如果我尝试像这样使用 $map 方法:

"projects.comment_count": { 
    "$map": { 
        "input": "$projects", 
        "as": "project", 
        "in": {
            "$size": "$$project.comments"
        } 
    } 
}

它将为数组中的每个对象返回一个如下所示的数组:

[{
    projects: [
        {
            _id: 1,
            comment_count: [3, 2, 0]
        },
        {
            _id: 2,
            comment_count: [3, 2, 0]
        },
        {
            _id: 3,
            comment_count: [3, 2, 0]
        }
    ]
}]

提前致谢!

【问题讨论】:

  • 在这里对您的问题没有多大帮助,但想说这是一个措辞恰当的问题,适合初学者

标签: javascript node.js mongodb aggregation-framework


【解决方案1】:

这是一个使用$unwind$group 然后$push$size 的想法。最后$project 摆脱那个_id

db.collection.aggregate([
  {
    "$unwind": "$projects"
  },
  {
    $group: {
      _id: null,
      "projects": {
        $push: {
          _id: "$projects._id",
          comment_count: {
            $size: "$projects.comments"
          }
        }
      }
    }
  },
  {
    "$project": {
      "_id": 0
    }
  }
])

你可以see the result here

【讨论】:

  • 代码中只有一个拼写错误,他想要带有连字符的“id”,所以 $push 命令中的代码应该是 _id:"$projects._id"。
  • 我最终使用了大部分代码。因为我有一个更长的聚合管道,可以从上游的多个其他集合中提取文档,所以我不得不放弃最后一个 $project 并在 $group 内构建我的最终输出,并参考使用 $$ROOT 的顶级文档。谢谢!
【解决方案2】:

您需要在 $map 聚合的in 参数中指定每个字段,最后将$sizecomments 数组一起使用。

类似的东西

db.collection.aggregate([
  { "$project": {
    "projects": {
      "$map": {
        "input": "$projects",
        "in": {
          "_id": "$$this._id",
          "comment_count": {
            "$size": "$$this.comments"
          }
        }
      }
    }
  }}
])

输出

[
  {
    "projects": [
      {
        "_id": 1,
        "comment_count": 3
      },
      {
        "_id": 2,
        "comment_count": 2
      },
      {
        "_id": 3,
        "comment_count": 0
      }
    ]
  }
]

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2017-09-13
    • 2021-03-13
    • 2015-07-30
    相关资源
    最近更新 更多