MongoDB Count()与聚合

dr.*_*tru 2 mongodb mongodb-query nosql-aggregation

我已经在mongo中使用了聚合,我知道在分组计数等方面的性能优势.但是,mongo在计算集合中所有文档的这两种方式上的性能有何不同?:

collection.aggregate([
  {
    $match: {}
  },{
    $group: {
      _id: null, 
      count: {$sum: 1}
    }
}]);
Run Code Online (Sandbox Code Playgroud)

collection.find({}).count()
Run Code Online (Sandbox Code Playgroud)

更新:第二种情况:假设我们有这样的样本数据:

{_id: 1, type: 'one', value: true}
{_id: 2, type: 'two', value: false}
{_id: 4, type: 'five', value: false}
Run Code Online (Sandbox Code Playgroud)

aggregate():

var _ids = ['id1', 'id2', 'id3'];
var counted = Collections.mail.aggregate([
  {
    '$match': {
      _id: {
        '$in': _ids
      },
      value: false
    }
  }, {
    '$group': {
      _id: "$type",
      count: {
        '$sum': 1
      }
    }
  }
]);
Run Code Online (Sandbox Code Playgroud)

count():

var counted = {};
var type = 'two';
for (i = 0, len = _ids.length; i < len; i++) {
  counted[_ids[i]] = Collections.mail.find({
    _id: _ids[i], value: false, type: type
  }).count();
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*erg 7

.count()到目前为止更快.您可以通过调用来查看实现

// Note the missing parentheses at the end
db.collection.count
Run Code Online (Sandbox Code Playgroud)

返回光标的长度.默认查询(如果count()没有查询文档调用),而后者实现为返回_id_索引的长度,iirc.

但是,聚合会读取每个文档并对其进行处理.这只能在相同的数量级上.count()进行,只需要在大约100k的文档上进行(根据你的RAM给出和接受).

以下函数应用于具有大约12M条目的集合:

function checkSpeed(col,iterations){

  // Get the collection
  var collectionUnderTest = db[col];

  // The collection we are writing our stats to
  var stats = db[col+'STATS']

  // remove old stats
  stats.remove({})

  // Prevent allocation in loop
  var start = new Date().getTime()
  var duration = new Date().getTime()

  print("Counting with count()")
  for (var i = 1; i <= iterations; i++){
    start = new Date().getTime();
    var result = collectionUnderTest.count()
    duration = new Date().getTime() - start
    stats.insert({"type":"count","pass":i,"duration":duration,"count":result})
  }

  print("Counting with aggregation")
  for(var j = 1; j <= iterations; j++){
    start = new Date().getTime()
    var doc = collectionUnderTest.aggregate([{ $group:{_id: null, count:{ $sum: 1 } } }])
    duration = new Date().getTime() - start
    stats.insert({"type":"aggregation", "pass":j, "duration": duration,"count":doc.count})
  }

  var averages = stats.aggregate([
   {$group:{_id:"$type","average":{"$avg":"$duration"}}} 
  ])

  return averages
}
Run Code Online (Sandbox Code Playgroud)

并返回:

{ "_id" : "aggregation", "average" : 43828.8 }
{ "_id" : "count", "average" : 0.6 }
Run Code Online (Sandbox Code Playgroud)

单位是毫秒.

心连心