mongodb 超出内存限制

mongodb 查询数据默认占用最大内存为100M,如果查询涉及到大量数据进行$group、$sort时会抛出类似以下的异常:

Received error in response from ins-mongodb-:: { $err: \"Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.\", code: 16945

秩序要在查询时添加{allowDiskUse: true}可以将查询结果保存到临时文件进行排序分组,来查看结果。

例如:

db.visits.aggregate(
[
{"$match":{"_id":{"$gte":ObjectId("5ae73d800000000000000000"),"$lt":ObjectId("5b101c000000000000000000")}}},
{"$group":{"_id":"$sip","cid":{"$addToSet":"$cid"},"count":{"$sum":1}}},
{"$project":{"_id":"$_id","cid":"$cid","cidcount":{"$size":"$cid"},"count":"$count"}},
{"$sort":{cidcount:-1}}
],{allowDiskUse: true}
)
上一篇:visual studio 中sstrcpy报错的问题


下一篇:T-SQL函数及用法--转