Mongoose-paginate:溢出sorting阶段缓冲的数据使用情况?

我正在使用mongoose,mongoose和ExpressJS。 我有这个错误:

错误检索stream:MongoError:运行器错误:溢出sorting阶段缓冲数据使用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中的这个数据限制问题?

编辑:

在这个答案之后 ,我认为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); 

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

编辑2:

有了这个解决scheme

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

我仍然得到相同的错误…

编辑3:

find排除字段的答案:

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

我仍然得到相同的错误…

您正在运行内存中sorting的32 MB限制。

你想创build一个索引,以便MongoDB为你sorting。

在MongoDB Shell中运行以下命令:

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

有关详细信息,请参阅此处: 溢出sorting阶段缓冲数据使用超过内部限制

编辑:要在您的回应中投射出一个字段,请按以下步骤操作:

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

在这里阅读: MongoDB显式排除

解决的问题 – 必须添加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}) 

使用光标来传输大数据集的数据:

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