我使用的是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'
};
我仍然得到同样的错误......
答案 0 :(得分:3)
您在内存中遇到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
答案 1 :(得分:0)
问题已解决 - 必须将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})
答案 2 :(得分:-1)
使用游标为大数据集流数据:
var findCursor = Stream.find(query).cursor();
findCursor.on("data", function(data) {
// datas...
});
findCursor.on("end", function(){
// end of the stream
});