Mongo聚合查询抛出错误:超出最大文档大小

时间:2018-04-26 16:50:49

标签: mongodb aggregate

我正在尝试使用 $ lookup $ filter 进行mongo聚合查询 该功能是获取角色列表以及相应的用户数。这里有很多用户大约13k,在一个角色中,它在mongo查询中执行该计数时抛出错误。

查询:

var aggregateObjArr = [
                        {$lookup: {
                                "localField": "_id",
                                "from": "Members",
                                "foreignField": "role",
                                "as": "membersInfo"
                            }
                        },
                        { $project: {
                                name: 1, preferredName: 1, isStaticRole: 1, createdBy: 1, privilegeCount: {$sum: {"$size": "$privilegeIds" }},
                                "membersInfo": {
                                    "$filter": {
                                        "input": "$membersInfo",
                                        "as": "child",
                                        "cond": { "$ne": ["$$child.status", -1]}
                                    }
                                }
                            }
                        },
                        { $project: {
                                name: 1, preferredName: 1, privilegeCount: 1, isStaticRole: 1, createdBy: 1, "userCount": { $sum: {"$size": "$membersInfo" }}
                            }
                        },
                        sortObj,
                        { $group: { _id: null, count: { $sum: 1 }, results: {'$push': '$$ROOT'}} },
                        { $project: { total: "$count", _id: 0, "results" : {$slice: ["$results", skipRows, limitRows]} }}
                    ];

collection.aggregate(aggregateObjArr, {"collation": {locale: "en", strength: 2}, "allowDiskUse" : true }, function (err, contentData) {

});

它会引发以下错误:

{ MongoError: Total size of documents in Members matching { $match: { $and: [ { role: { $eq: ObjectId('58949cedc7428a346f097ff8') } }, {} ] } } exceeds maximum document size
    at Function.MongoError.create (/home/www/ssnw/v2/node_modules/mongodb-core/lib/error.js:31:11)
    at /home/www/ssnw/v2/node_modules/mongodb-core/lib/connection/pool.js:497:72
    at authenticateStragglers (/home/www/ssnw/v2/node_modules/mongodb-core/lib/connection/pool.js:443:16)
    at Connection.messageHandler (/home/www/ssnw/v2/node_modules/mongodb-core/lib/connection/pool.js:477:5)
    at Socket.<anonymous> (/home/www/ssnw/v2/node_modules/mongodb-core/lib/connection/connection.js:331:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:547:20)
  name: 'MongoError',
  message: 'Total size of documents in Members matching { $match: { $and: [ { role: { $eq: ObjectId(\'58949cedc7428a346f097ff8\') } }, {} ] } } exceeds maximum document size',
  ok: 0,
  errmsg: 'Total size of documents in Members matching { $match: { $and: [ { role: { $eq: ObjectId(\'58949cedc7428a346f097ff8\') } }, {} ] } } exceeds maximum document size',
  code: 4568,
  codeName: 'Location4568' }

我无法使用 $ match 条件而不是 $ fliter ,因为某些角色不存在任何用户。 有人可以帮忙吗?

0 个答案:

没有答案