mongodb - map减少和查找



是否有可能在同一查询管道中高效地执行map reduce和查找?

假设我有两个集合:

  • 项目:{ _id, group_id, createdAt }
  • 采购:{ _id, item_id }

我想根据每组最近购买的x个商品的数量获得前n个商品组。

如果我有项目文档中可用的购买数量,那么我可以汇总和排序,但事实并非如此。

我可以得到每组最近的x项,如下:

let x = 3;
let map = function () {
  emit(this.group_id, { items: [this] });
};
let reduce = function (key, values) {
  return { items: getLastXItems(x, values.map(v => v.items[0])) };
};
let scope = { x };
db.items.mapReduce(map, reduce, { out: { inline: 1 }, scope }, function(err, res) {
  if (err) {
    ...
  } else {
    // res is an array of { group_id, items } where items is the last x items of the group
  }
});

但是我缺少购买计数,所以我不能用它来排序组,并输出前n组(顺便说一句,我甚至不确定我能做到)

我在web服务器上使用这个,并根据用户上下文使用范围变量运行查询,所以我不想将结果输出到另一个集合,并且必须做内联的一切。

===编辑1 ===添加数据示例:

样本数据可以是:

// items
{ _id: '1, group_id: 'a', createdAt: 0 }
{ _id: '2, group_id: 'a', createdAt: 2 }
{ _id: '3, group_id: 'a', createdAt: 4 }
{ _id: '4, group_id: 'b', createdAt: 1 }
{ _id: '5, group_id: 'b', createdAt: 3 }
{ _id: '6, group_id: 'b', createdAt: 5 }
{ _id: '7, group_id: 'b', createdAt: 7 }
{ _id: '8, group_id: 'c', createdAt: 5 }
{ _id: '9, group_id: 'd', createdAt: 5 }
// purchases
{ _id: '1', item_id: '1' }
{ _id: '2', item_id: '1' }
{ _id: '3', item_id: '3' }
{ _id: '4', item_id: '5' }
{ _id: '5', item_id: '5' }
{ _id: '6', item_id: '6' }
{ _id: '7', item_id: '7' }
{ _id: '8', item_id: '7' }
{ _id: '9', item_id: '7' }
{ _id: '10', item_id: '3' }
{ _id: '11', item_id: '9' }

n = 3x = 2的样品结果为:

[
  group_id: 'a', numberOfPurchasesOnLastXItems: 4,
  group_id: 'b', numberOfPurchasesOnLastXItems: 3,
  group_id: 'c', numberOfPurchasesOnLastXItems: 1,
]

我认为这可以通过聚合管道解决,但我不知道这有多糟糕,特别是性能方面。

我担心的是:

  • 聚合管道是否能够从索引、查找和排序中获益?
  • 可以简化仅用于计数匹配项的查找+投影吗

无论如何,我认为我可以有一个解决方案:

x = 2;
n = 3;
items.aggregate([
  {
    $lookup: {
      from: 'purchases',
      localField: '_id',
      foreignField: 'item_id',
      as: 'purchases',
    },
  },
  /*
  after the join, the data is like {
    _id: <itemId>,
    group_id: <itemGroupId>,
    createdAt: <itemCreationDate>,
    purchases: <arrayOfPurchases>,
  }
  */
  {
    $project: {
      group_id: 1,
      createdAt: 1,
      pruchasesCount: { $size: '$purchases' },
    }
  }
  /*
  after the projection, the data is like {
    _id: <itemId>,
    group_id: <itemGroupId>,
    createdAt: <itemCreationDate>,
    purchasesCount: <numberOfPurchases>,
  }
  */
  {
    $sort: { createdAt: 1 }
  },
  {
    $group: {
      _id: '$group_id',
      items: {
        $push: '$purchasesCount',
      }
    }
  }
  /*
  after the group, the data is like {
    _id: <groupId>,
    items: <array of number of purchases per item, sorted per item creation date>,
  }
  */
  {
    $project: {
      numberOfPurchasesOnMostRecentItems: { $sum: { $slice: ['$purchasesCount', x] } },
    }
  }
  /*
  after the projection, the data is like {
    _id: <groupId>,
    numberOfPurchasesOnMostRecentItems: <number of purchases on the last x items>,
  }
  */
  {
    $sort: { numberOfPurchasesOnMostRecentItems: 1 }
  },
  { $limit : n }
]);

相关内容

  • 没有找到相关文章