Storing long string in MongoDB causes insertion error

Viewed 947

I'm building a forum-like web app for my university, and one of the features I'm implementing is the CRUD functionality for users in writing our answers for a specific exam paper. However, I found out during testing that I can't store strings in the 'details' part of an answers document due to an index error, as shown.

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

I'm using Express with MongoDB, so I chose mongoose to be my MongoDB driver. The schema for the 'answer' document is as shown:

'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);
};

As you can see, I did not index the 'details' element of the 'answer' document, but the error still occurs. Is there a way around this?

0 Answers
Related