猫鼬不会保存庞大的文字

时间:2015-11-17 22:08:07

标签: javascript node.js mongodb mongoose

我正在尝试将一个非常大的文本保存到我的mongodb数据库中,但它崩溃了。我正在尝试构建一个代码段管理器,这是我的错误代码:

{ [MongoError: Btree::insert: key too large to index, failing beverages.recipes.$code_1 3271 { : "var gulp = require('gulp');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var browserSync = require('browser-sy..." }]
  name: 'MongoError',
  message: 'Btree::insert: key too large to index, failing beverages.recipes.$code_1 3271 { : "var gulp = require(\'gulp\');\nvar source = require(\'vinyl-source-stream\');\nvar browserify = require(\'browserify\');\nvar browserSync = require(\'browser-sy..." }',
  driver: true,
  code: 17280,
  index: 0,
  errmsg: 'Btree::insert: key too large to index, failing beverages.recipes.$code_1 3271 { : "var gulp = require(\'gulp\');\nvar source = require(\'vinyl-source-stream\');\nvar browserify = require(\'browserify\');\nvar browserSync = require(\'browser-sy..." }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }

这是我的猫鼬模型。

import mongoose from 'mongoose';

const Schema = mongoose.Schema;

let recipeSchema = new Schema({
  title: { type: String, required: true  },
  author: { type: String},
  tags: { type: String, required: true },
  code: { type: String, required: true, unique: true, index: true },
  created_at: Date,
  updated_at: Date
});

recipeSchema.pre('save', function(next) {

  var recipe = this;
  // get the current date
  var currentDate = new Date();

  // change the updated_at field to current date
  recipe.updated_at = currentDate;

  // if created_at doesn't exist, add to that field
  if (!recipe.created_at){
    recipe.created_at = currentDate;
  }

  next();
});

let Recipe = mongoose.model('Recipe', recipeSchema);

export default Recipe;

如果是found this on mongodb,但我不知道如何使用mongoose来使用它。这是正确的做法吗?

这是我试图保存的文件

{ title: 'test',
  tags: 'test',
  code: 'var gulp = require(\'gulp\');\nvar source = require(\'vinyl-source-stream\');\nvar browserify = require(\'browserify\');\nvar browserSync = require(\'browser-sync\');\nvar nodemon = require(\'gulp-nodemon\');\nvar babelify = require(\'babelify\');\nvar eslint = require(\'gulp-eslint\');\nvar buffer = require(\'vinyl-buffer\');\nvar uglify = require(\'gulp-uglify\');\nvar sourcemaps = require(\'gulp-sourcemaps\');\nvar sass = require(\'gulp-ruby-sass\');\nvar config = require(\'./config\');\nvar imageop = require(\'gulp-image-optimization\');\nvar concat = require(\'gulp-concat\');\n\ngulp.task(\'images\', function(cb) {\n  gulp.src(\'./src/assets/images/**/*\')\n    .pipe( imageop({\n      optimizationLevel: 5,\n      progressive: true,\n      interlaced: true\n    }))\n    .pipe(gulp.dest(\'./public/images/\'));\n});\n\n\ngulp.task(\'browser-sync\', [\'nodemon\'], function() {\n  browserSync({\n    files: [\'public/**/*.*\'],\n    proxy: "localhost:" + config.APP_PORT,  // local node app address\n    port: 5000,  // use *different* port than above\n    notify: true,\n    browser: "google chrome",\n  });\n});\n\ngulp.task(\'nodemon\', function(cb) {\n  var called = false;\n  return nodemon({\n    script: \'./index.js\',\n    ignore: [\n      \'gulpfile.js\',\n      \'node_modules/\',\n      \'public/,\'\n    ]\n  })\n    .on(\'start\', function () {\n      if (!called) {\n        called = true;\n        cb();\n      }\n    })\n    .on(\'restart\', function () {\n      setTimeout(function () {\n        browserSync.reload({ stream: false });\n      }, 200);\n    });\n});\n\n\ngulp.task(\'browserify\', function () {\n  return browserify({entries: \'./src/client/app.js\', extensions: [\'.js\'], debug: true})\n    .transform(babelify)\n    .bundle()\n    .pipe(source(\'app.min.js\'))\n    .pipe(buffer())\n    .pipe(sourcemaps.init({loadMaps: true}))\n    // Add transformation tasks to the pipeline here.\n    .pipe(uglify())\n    .pipe(sourcemaps.write(\'./\'))\n    .pipe(gulp.dest(\'./public/js\'));\n});\n\ngulp.task(\'lint\', function (cb) {\n  return gulp.src([\'./src/**/*.js\'])\n    .pipe(eslint())\n    .pipe(eslint.format())\n    .pipe(eslint.failOnError());\n});\n\ngulp.task(\'sass\', function () {\n  return sass(\'./src/assets/sass/style.sass\', { sourcemap: true,  style: \'compressed\' })\n    .on(\'error\', sass.logError)\n    .pipe(sourcemaps.write(\'./\', {\n      includeContent: false,\n      sourceRoot: \'source\'\n    }))\n    .pipe(gulp.dest(\'./public/css/\'));\n});\n\ngulp.task(\'scripts\', function(){\n  return gulp.src([\'./src/assets/js/bootstrap.min.js\',\'./src/assets/js/checkbox.js\',\'./src/assets/js/radio.js\',\'./src/assets/js/bootstrap-switch.js\',\'./src/assets/js/toolbar.js\',\'./src/assets/js/application.js\'])\n    .pipe(sourcemaps.init())\n    .pipe(concat(\'assets.min.js\'))\n    .pipe(gulp.dest(\'./public/js/\'))\n    .pipe(uglify())\n    .pipe(sourcemaps.write(\'./\'))\n    .pipe(gulp.dest(\'./public/js/\'));\n});\n\ngulp.task(\'fonts\', function() {\n  return gulp.src(\'./src/assets/fonts/**/*\').pipe(gulp.dest(\'./public/fonts/\'));\n});\n\ngulp.task(\'watch\', function() {\n  gulp.watch([\'./src/**/*.js\'], [\'lint\', \'browserify\', \'scripts\', browserSync.reload]);\n  gulp.watch([\'./src/assets/sass/**/*.sass\'], [\'sass\', browserSync.reload]);\n});\n\ngulp.task(\'serve\', [\'browser-sync\', \'watch\']);\n\ngulp.task(\'default\', [\'browserify\', \'nodemon\', \'sass\', \'scripts\', \'fonts\', \'images\', \'watch\']);' }

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:3)

您似乎正在尝试使用字段code作为索引。根据您的链接,code值的大小约为3KB。 Mongo的限制为index fields must not exceed 1KB。 3> 1因此,您收到此错误。

您可以通过以下方式解决此问题:

  1. 缩小您输入的文字的大小
  2. 未在code字段
  3. 上建立索引

答案 1 :(得分:0)

对于索引代码字段,请使用文本索引。对于唯一使用哈希表单代码。 例如:

import crypto  from 'crypto';

let recipeSchema = new Schema({
  title: { type: String, required: true  },
  author: { type: String},
  tags: { type: String, required: true },
  code: { type: String, required: true },
  codeHash: {type: String, unique: true },
  created_at: Date,
  updated_at: Date
});

recipeSchema.index({code: 'text'}

recipeSchema.pre('save', function( 

  var recipe = this;
  // get the current date
  var currentDate = new Date();

  // change the updated_at field to current date
  recipe.updated_at = currentDate;

  // if created_at doesn't exist, add to that field
  if (!recipe.created_at){
    recipe.created_at = currentDate;
  }

  var hash = crypto.createHash('md5').update(this.code).digest("hex")
  this.codeHash = hash;
  next();
});