在MongoDB中存储长字符串会导致插入错误

时间:2017-07-17 11:01:15

标签: node.js mongodb express mongoose mongoose-schema

我正在为我的大学构建一个类似论坛的网络应用程序,我正在实现的功能之一是用户在撰写特定试卷的答案时的CRUD功能。但是,我在测试期间发现,由于索引错误,我无法在答案文档的“详细信息”部分存储字符串,如图所示。

 {"success":false,"errors":["Exception: WriteError({\"code\":17280,\"index\":0,\"errmsg\":\"Btree::insert: key too large to index, failing maths_pyp.answers.$details_1 2265 { : \\\"....

我正在使用Express与MongoDB,所以我选择mongoose作为我的MongoDB驱动程序。 “回答”文档的架构如下所示:

'use strict';

exports = module.exports = function(app, mongoose) {
  var answerSchema = new mongoose.Schema({
    pivot: { type: String, default: '' },
    module_code: {type: String, required: true},
    _year_sem: {type: String, require: true},
    author: { type: String, required:true},
    author_id: {type: String, required: true},
    question_number: {type: String, required: true},
    details: {type: String, require: true},
    date: {type: Date, default: Date.now}
  });
  answerSchema.plugin(require('./plugins/pagedFind'));
  answerSchema.index({ pivot: 1 });
  answerSchema.index({ module_code: 1 });
  answerSchema.index({ _year_sem: 1 });
  answerSchema.index({ author: 1 });
  answerSchema.index({ author_id: 1 });
  answerSchema.index({ question_number: 1 });
  answerSchema.index({ date: 1 });
  answerSchema.set('autoIndex', (app.get('env') === 'development'));
  app.db.model('Answer', answerSchema);
};

如您所见,我没有索引“回答”文档的“详细信息”元素,但错误仍然存​​在。有办法解决这个问题吗?

0 个答案:

没有答案