【问题标题】:Mongoose-paginate: Overflow sort stage buffered data usage?Mongoose-paginate:溢出排序阶段缓冲数据使用情况?
【发布时间】:2017-03-23 10:58:23
【问题描述】:

我正在使用 Mongoose、Mongoose-paginate 和 ExpressJS。我有这个错误:

检索流时出错:MongoError: Runner 错误:溢出排序 阶段缓冲数据使用量 34227161 字节超出内部限制 33554432 字节

routes.js:

router.get("/", function(req, res, next) {

    var page = req.query.page === undefined ? 1 : req.query.page;
    var options = {
        page: page,
        limit: 30,
        sort: {
            created_at: 'desc'
        }
    };

    // https://github.com/edwardhotchkiss/mongoose-paginate
    Stream.paginate(query, options, function(err, result) {
        if (err) {
            console.log("Error retrieving streams: " + err);
            errorMessage = "A problem occurred retrieving the streams";
            return res.render("streams", {
                streams: {},
                errorMessage: errorMessage
            });
        }

        return res.render("streams/list", {
            streams: result.docs,
            page: parseInt(result.page),
            pages: parseInt(result.pages)
        });
    });
});

如何解决 Mongoose 中的数据限制问题?

编辑:

answer 之后,我认为data 字段中的大小变得太大,那么如何索引data 字段?

这是我的模型:

model.js:

var mongoose = require("mongoose");
var mongoosePaginate = require('mongoose-paginate');

// Declare schema
var streamSchema = new mongoose.Schema({
    user_id: {
        type: String,
        required: true
    },
    title: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    hidden:{
        type: String
    },
    data: {
        type: Object
    }
});

streamSchema.plugin(mongoosePaginate);

// Export schema
// Model.paginate()
mongoose.model("Stream", streamSchema);

当我查询流列表时,我可以忽略data字段吗?

编辑 2:

使用此解决方案:

db.mydb.ensureIndex({created_at: 1})
{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

我仍然遇到同样的错误...

编辑 3:

找到排除字段的答案:

var options = {
    page: page,
    limit: 30,
    sort: {
        created_at: 'desc'
    },
    select: '-data'
};

我仍然遇到同样的错误...

【问题讨论】:

标签: node.js mongodb express mongoose


【解决方案1】:

您在内存中遇到了 32 MB 的排序限制。

您想创建一个索引,以便 MongoDB 为您进行排序。

在您的 MongoDB Shell 中运行以下命令:

db.collectionName.ensureIndex({created_at: 1})

请参阅此处了解更多信息: Overflow sort stage buffered data usage exceeds internal limit

编辑:要在您的回复中突出一个字段,您可以按以下方式进行操作:

db.collection.find({}, {field_to_see: 1, field_to_hide: 0})

在这里阅读:MongoDB Explicit Exclusion

【讨论】:

  • 查询流时如何忽略字段?请参阅我上面的编辑。谢谢。
  • db.collectionName.ensureIndex({created_at: 1}) 这是做什么的?
  • 它告诉 MongoDB 按该字段对您的集合进行排序,以便它可以更快地查询您的数据,并尽可能避免在内存中对其进行排序。
  • 感谢您的编辑。问题是我正在使用mongoose-paginate - 我如何使用db.collection.find({}, {field_to_see: 1, field_to_hide: 0})
  • Stream.paginate(query, options, function(err, result) { 这就是我查询和分页流的方式。
【解决方案2】:

使用光标为大数据集流式传输数据:

var findCursor = Stream.find(query).cursor();
findCursor.on("data", function(data) {
  // datas...
});
findCursor.on("end", function(){
 // end of the stream
});

【讨论】:

  • 查询流时如何忽略字段?请参阅我上面的编辑。谢谢。
  • 忽略字段,使用项目字段:Stream.find(query, { data:0}) 见docs.mongodb.com/v3.2/tutorial/…
【解决方案3】:

问题已解决 - 必须在模型中添加 index: 1

var mongoose = require("mongoose");
var mongoosePaginate = require('mongoose-paginate');

// Declare schema
var streamSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    hidden:{
        type: String
    },
    data: {
        type: Object
    },
    created_at: {
        type: Date,
        default: Date.now,
        index: 1
    },
});

streamSchema.plugin(mongoosePaginate);

// Export schema
// Model.paginate()
mongoose.model("Stream", streamSchema);

然后:

mongo> db.mydb.ensureIndex({created_at: 1})

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 2016-01-15
    • 2015-01-17
    • 2015-08-03
    • 1970-01-01
    • 2019-04-14
    • 2016-01-08
    相关资源
    最近更新 更多