【问题标题】:MongoDB aggregation group by similar stringMongoDB 聚合组按相似字符串
【发布时间】:2019-03-03 03:14:46
【问题描述】:

我开始为 Mongo 学习聚合,但在我的项目中,我发现我的收藏中有很多名称非常相似的品牌,例如“BrandA”和“BrandA tech”。有没有办法在我的聚合结束时对它们进行分组?

我的数据库中有 2 个集合:

第一个是针对品牌的:

{
  _id: ObjectId(),
  name: String
}

第二个是针对产品的:

{
  _id: ObjectId(),
  name: String,
  brand: ObjectId() // referring to _id of brands
}

现在假设我有以下品牌:

{_id: ObjectId('5a9fd2b8045b020013de2a47'), name: 'brand1'},
{_id: ObjectId('5a9fcf94d28420245451a39c'), name: 'brand2'},
{_id: ObjectId('5a9fcf94d28420245451a39a'), name: 'brand1 sub1'},
{_id: ObjectId('5a9fe8bf045b020013de2a6d'), name: 'sub2 brand2'}

以及以下产品:

{_id: ObjectId(''), name: 'item1', brand: ObjectId('5a9fd2b8045b020013de2a47')},
{_id: ObjectId(''), name: 'item2', brand: ObjectId('5a9fcf94d28420245451a39c')},
{_id: ObjectId(''), name: 'item3', brand: ObjectId('5a9fd2b8045b020013de2a47')},
{_id: ObjectId(''), name: 'item4', brand: ObjectId('5a9fcf94d28420245451a39a')},
{_id: ObjectId(''), name: 'item5', brand: ObjectId('5a9fe8bf045b020013de2a6d')},
{_id: ObjectId(''), name: 'item6', brand: ObjectId('5a9fd2b8045b020013de2a47')},
{_id: ObjectId(''), name: 'item7', brand: ObjectId('5a9fcf94d28420245451a39c')},
{_id: ObjectId(''), name: 'item8', brand: ObjectId('5a9fcf94d28420245451a39a')}

我现在的查询:

db.getCollection('products').aggregate([
  {$group: {
    _id: '$brand',
    amount: { $sum: 1 },
  }},
  {
    $sort: { 'amount': -1 }
  },{$lookup: {
    from: 'brands',
    localField: '_id',
    foreignField: '_id',
    as: 'lookup'
  }},
  {$unwind: {path: '$lookup'}},
  {$project: {
    _id: '$_id',
    brandName: '$lookup.name',
    amount: '$amount'
  }}
]);

结果:

{_id: ObjectId('5a9fd2b8045b020013de2a47'), brandName: 'brand1', amount: 3}
{_id: ObjectId('5a9fcf94d28420245451a39c'), brandName: 'brand2', amount: 2}
{_id: ObjectId('5a9fcf94d28420245451a39a'), brandName: 'brand1 sub1', amount: 2}
{_id: ObjectId('5a9fe8bf045b020013de2a6d'), brandName: 'sub2 brand2', amount: 1}

我想要的结果:

{_id: ObjectId(null), brandName: 'brand1', amount: 5},
{_id: ObjectId(null), brandName: 'brand2', amount: 3}

是否可以通过在品牌名称中找到类似的字符串来对我现在的结果进行分组?喜欢将 'brand1' 和 'brand1 sub1' 或 'brand2' 和 'sub2 brand2' 分组?

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    我认为你可以通过使用$split$unwind 来做你想做的事

    split 会将您的字符串转换为单词数组,unwind 将创建与数组中单词一样多的条目。

    然后您可以应用您已经准备好的管道来计算出现次数。

    【讨论】:

      【解决方案2】:

      模型的改变可以很容易地实现这一点。只需将数组中的项目添加到品牌。 然后使用数组的长度立即得到计数,查询速度更快。

      【讨论】:

      • 是的,我也是这么想的,但这是我实习所在公司数据库的一个非常精简的版本。因此,不幸的是,建议在第 4 天更改模型可能不是解决问题的最佳方法://
      • 那么我建议使用 $regex mongoosejs.com/docs/api.html#model_Model.count docs.mongodb.com/manual/reference/operator/query/regex 计数来计算列数。正则表达式用于查询部分字符串。
      • 是的,谢谢!我想我会这样做 :) 唯一不好的是我必须运行不同的查询:/
      • 如果你想聚合查询,你应该看看 mongoose 聚合,它可以计算查询。
      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      相关资源
      最近更新 更多