查询嵌套数组中的元素总数-嵌入文档MongoDB

查询嵌套数组中的元素总数-嵌入文档MongoDB,mongodb,mongodb-query,aggregation-framework,nosql,Mongodb,Mongodb Query,Aggregation Framework,Nosql,我的收藏中有以下文档: { _id: 1, activities: [ { activity_id: 1, travel: [ { point_id: 1, location: [-76.0,19.1] }, { point_id: 2, location: [-77.0,19.3] } ] }

我的收藏中有以下文档:

{
  _id: 1,
  activities: [
    {
      activity_id: 1,
      travel: [
        {
          point_id: 1,
          location: [-76.0,19.1]
        },
        {
          point_id: 2,
          location: [-77.0,19.3]
        }
      ]
    },
    {
      activity_id: 2,
      travel: [
        {
          point_id: 3,
          location: [-99.3,18.2]
        }
      ]
    }
  ]
},
{
  _id: 2,
  activities: [
    {
      activity_id: 3,
      travel: [
        {
          point_id: 4,
          location: [-75.0,11.1]
        }
      ]
    }
  ]
}
我可以得到活动的总数,如下所示:

db.mycollection.aggregate(
  {$unwind: "$activities"}, 
  {$project: {count:{$add:1}}}, 
  {$group: {_id: null, number: {$sum: "$count" }}}
)
我得到(3项活动):

问题:如何获取所有行程中的元素总数

预期结果:
4
元素

这些是:

{
  point_id: 1,
  location: [-76.0,19.1]
},
{
  point_id: 2,
  location: [-77.0,19.3]
},
{
  point_id: 3,
  location: [-99.3,18.2]
},
{
  point_id: 4,
  location: [-75.0,11.1]
}

您可以使用double
$unwind

e、 g

这将发出非常接近您所需的输出格式:

{ 
    "travel" : [
        {
            "point_id" : 1.0, 
            "location" : [
                -76.0, 
                19.1
            ]
        }, 
        {
            "point_id" : 2.0, 
            "location" : [
                -77.0, 
                19.3
            ]
        }, 
        {
            "point_id" : 3.0, 
            "location" : [
                -99.3, 
                18.2
            ]
        }, 
        {
            "point_id" : 4.0, 
            "location" : [
                -75.0, 
                11.1
            ]
        }
    ]
}
更新:

如果你只想知道整个藏品中旅行证件的总数

尝试:

它将打印:

{ 
    "_id" : NumberInt(0), 
    "total" : NumberInt(4)
}
更新2:

OP希望根据聚合框架中的某些属性筛选文档。以下是一种方法:

db.collection.aggregate([
  {$unwind: "$activities"},
  {$match:{"activities.activity_id":1}},
  {$unwind: "$activities.travel"},
  {$group: {_id:0, total:{$sum:1}}}
])
它将打印(基于示例文档):


您可以使用double
$unwind

e、 g

这将发出非常接近您所需的输出格式:

{ 
    "travel" : [
        {
            "point_id" : 1.0, 
            "location" : [
                -76.0, 
                19.1
            ]
        }, 
        {
            "point_id" : 2.0, 
            "location" : [
                -77.0, 
                19.3
            ]
        }, 
        {
            "point_id" : 3.0, 
            "location" : [
                -99.3, 
                18.2
            ]
        }, 
        {
            "point_id" : 4.0, 
            "location" : [
                -75.0, 
                11.1
            ]
        }
    ]
}
更新:

如果你只想知道整个藏品中旅行证件的总数

尝试:

它将打印:

{ 
    "_id" : NumberInt(0), 
    "total" : NumberInt(4)
}
更新2:

OP希望根据聚合框架中的某些属性筛选文档。以下是一种方法:

db.collection.aggregate([
  {$unwind: "$activities"},
  {$match:{"activities.activity_id":1}},
  {$unwind: "$activities.travel"},
  {$group: {_id:0, total:{$sum:1}}}
])
它将打印(基于示例文档):


是否可以添加参数
{$match:{“activities.activity_id::1}}
,以便获取特定活动的旅行列表。。。。怎么做?你确实可以做到。请参见$matchyes,但是。。。。。这不起作用,看。。。。我想我将不得不使用关系表,而不是嵌入式对象。我相信这是可行的。现在我没有访问机器的权限,但会尽快更新。我发现了我的错误,
$match
$unwind
参数的顺序是关键的。可以添加参数
{$match:{“activities.activity\u id::1}
,这样我就可以得到特定活动的旅行列表了。。。。怎么做?你确实可以做到。请参见$matchyes,但是。。。。。这不起作用,看。。。。我想我将不得不使用关系表,而不是嵌入式对象。我相信这是可行的。现在我没有访问机器的权限,但会尽快更新。我发现了我的错误,
$match
$unwind
参数的顺序是关键的。实际上,这过去和现在都像
db.mycollection.aggregate({“$group”:{“\u id”:null,“total”:{“$sum”:{“$sum”:{“$map”:{“input”:“$activities”,“as”:“a”,“in”:{“$size”:“$$a.travel”}}}})
。这是因为自从MongoDB 3.2以来,
$sum
可以直接与数组一起工作,也可以作为累加器。根据您自己的删除答案,此时您的MongoDB版本似乎比这要旧得多。实际上,这与
db.mycollection.aggregate({“$group”){“\u id”:null,“total”:{$sum:{“$sum”:{“$map”:{“input”:“$activities”,“as”:“a”,“in”:{“$size”:“$$a.travel”}}}}}}}}}}}}}}}}
。这是因为,
$sum
可以直接与数组一起工作,也可以作为MongoDB 3.2以来的累加器。根据您自己删除的答案,此时您的MongoDB版本似乎比这要旧得多。
{ "_id" : 0, "total" : 2 }