【问题标题】:MongoDB $match $group query slowMongoDB $match $group 查询慢
【发布时间】:2020-03-21 07:05:24
【问题描述】:

所以我对大约 500k 文档进行了以下查询,并且在数据库方面确实很慢:

db.collection('news').aggregate(
    [
      {
        $match: {
          feeds: { $in: feeds },
          createdAt: { $lte: lastPostDate },
          deleted: {
            $exists: false
          }
        }
      },
      {
        $group: {
          _id: { title: '$title' },
          id: { $first: '$_id' },
          title: { $addToSet: '$title' },
          source: { $first: '$source' },
          shortid: { $first: '$shortid' },
          stats: { $first: '$stats' },
          log: { $first: '$log' },
          createdAt: { $first: '$createdAt' },
          feedUpdated: { $first: '$feedUpdated' },
          media: { $first: '$media' },
          title: { $first: '$title' }
        }
      },
      { $sort: sort },
      {
        $skip: limit * (page - 1)
      },
      {
        $limit: limit * 1
      }
    ],
    {
      allowDiskUse: true,
      cursor: {}
    }
)

我知道 allowDiskUse 让它变慢了,但如果我禁用它,我会得到:

MongoError: 超出 $group 的内存限制,但不允许 外部排序。传递 allowDiskUse:true 以选择加入。

【问题讨论】:

    标签: mongodb mongodb-query mongodb-indexes


    【解决方案1】:
    let aggregation = MongoSchemaModel.aggregate([]); 
    aggregation.options = { allowDiskUse: true }; 
    aggregation.exec((err, data) => {});
    

    let aggregation = MongoSchemaModel.aggregate([], { allowDiskUse: true });
    aggregation.exec((err, data) => {});
    

    参考:- https://mkyong.com/mongodb/mongodb-sort-exceeded-memory-limit-of-104857600-bytes/

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 2019-03-25
      • 2017-11-25
      • 2013-05-30
      • 2020-08-06
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多