mongoose不会保存大文本

我试图保存一个非常大的文本到我的MongoDB数据库,但它崩溃了。 我试图build立一个片段pipe理器,这是我的错误代码:

{ [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] } 

这是我的mongoose模型。

 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; 

如果发现这在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\']);' } 

非常感谢您的帮助!

看起来你正在尝试使用字段code作为你的索引。 根据你的链接, code值的大小是〜3KB。 Mongo有一个限制,即索引字段不能超过1KB 。 3> 1,所以你得到这个错误。

你可以通过以下两种方法解决

  1. 减less您input的文字的大小
  2. 不在code字段编制索引

索引代码字段使用文本索引。 为了独特使用散列表单代码。 例如:

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