【问题标题】:MongoDB use two unwind on aggregate for getting the value of repetition countMongoDB 在聚合上使用两个展开来获取重复计数的值
【发布时间】:2018-10-24 16:37:32
【问题描述】:

我有一个这样的数据集:

{
    "_id" : ObjectId("5bacc9295af10e2764648baa"),
    "slug" : ["Maruti", "Honda"],
    "page" : "Ford"
},
{
    "_id" : ObjectId("5bacc9295af10e2764648bab"),
    "slug" : ["Maruti", "Honda", "Tata"],
    "page" : "Hyundai"
},
{
    "_id" : ObjectId("5bacc9295af10e2764648bac"),
    "slug" : ["Maruti"],
    "page" : "Ford"
},
{
    "_id" : ObjectId("5bacc9295af10e2764648bad"),
    "slug" : ["Ford", "Hyundai"],
    "page" : "Tata"
}

现在,如果我想获得 Page 的重复次数,那么我将像这样进行聚合查询:

MyCollectionName.aggregate([
      { $unwind: { path: "$page" } },
      { $group: { _id: "$page", count: { $sum: 1 } } },
      {
        $project: {
          _id: 0,
          vehiclename: "$_id",
          count: { $multiply: ["$count", 1] }
        }
      },
      { $sort: { count: -1 } }
    ])
    .then(data => {
         console.log(data)
         //get the result like this which is fine
         [
           { vehiclename : 'Ford', count: 2},
           { vehiclename : 'Hyundai', count: 1},
           { vehiclename : 'Tata', count: 1}
         ]
     })
     .catch(e => {
       console.log(e)
      })

如果我为 Slug 做同样的事情,那么我的查询将是这样的:

MyCollectionName.aggregate([
      { $unwind: { path: "$slug" } },
      { $group: { _id: "$slug", count: { $sum: 1 } } },
      {
        $project: {
          _id: 0,
          vehiclename: "$_id",
          count: { $multiply: ["$count", 1] }
        }
      },
      { $sort: { count: -1 } }
    ])
    .then(data => {
         console.log(data)
         //get the result like this which is fine
         [
           { vehiclename : 'Maruti', count: 3},
           { vehiclename : 'Honda', count: 2},
           { vehiclename : 'Tata', count: 1},
           { vehiclename : 'Ford', count: 1},
           { vehiclename : 'Hyundai', count: 1}
         ]
     })
     .catch(e => {
       console.log(e)
      })

现在我想在单个查询而不是单独查询上执行此操作。

我对使用 unwind 以及在单个查询中获得两个组合值后感到有些困惑。

所需的输出将是这样的:

[
 { vehiclename : 'Maruti', count: 3},
 { vehiclename : 'Ford', count: 3},
 { vehiclename : 'Honda', count: 2},
 { vehiclename : 'Tata', count: 2},        
 { vehiclename : 'Hyundai', count: 1}
]

非常感谢任何帮助。

【问题讨论】:

    标签: mongodb aggregation


    【解决方案1】:

    我得到了解决方案。如果我做错了什么,请通知..

    MyCollectionName.aggregate([
          {
            $facet: {
              groupByPage: [
                { $unwind: "$page" },
                {
                  $group: {
                    _id: "$page",
                    count: { $sum: 1 }
                  }
                }
              ],
              groupBySlug: [
                { $unwind: "$slug" },
                {
                  $group: {
                    _id: "$slug",
                    count: { $sum: 1 }
                  }
                }
              ]
            }
          },
          {
            $project: {
              pages: {
                $concatArrays: ["$groupByPage", "$groupBySlug"]
              }
            }
          },
          { $unwind: "$pages" },
          {
            $group: {
              _id: "$pages._id",
              count: { $sum: "$pages.count" }
            }
          },
          { $sort: { count: -1 } }
        ])
        .then(data => {
             console.log(data)             
         })
         .catch(e => {
           console.log(e)
          })
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 2015-09-12
      • 1970-01-01
      • 2019-07-13
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      相关资源
      最近更新 更多