首页 > 解决方案 > 如何在 mongoDB 集合字段中保存大型 html 内容(文章)?

问题描述

我想将大型 html 内容保存在 mongodb 中,但它不允许在单个集合字段中保存超过 1 kb 的内容。基本上我正在使用 NUXT 构建一个文章提交网络应用程序,我选择的数据库是 Mongodb。现在我被困在这个关键的任务中。

任何帮助都会很棒!

var mongoose = require('mongoose');
var ArticleSchema = new mongoose.Schema({
    title : { type : String , unique : true, required : true},
    description: { type : String , unique : true, required : true},
    content: { type : String , unique : true, required : true},
    banner: { type : String },
    active: { type: Boolean, default: false },
    created_date: { type: Date, default: Date.now },
    updated_date: { type: Date, default: Date.now },
    postedBy: {
        type : Object , required : true
    },
    comments: [{
        text: String,
        postedBy: {
            type : Object , required : true
        }
    }]
  });
module.exports = mongoose.model('Article', ArticleSchema);

保存大型内容时出现此错误:

Btree::insert: key too large to index, failing tech-expert.articles.$content_1 3500 { : \"<p>Here is Sample text!Here is Sample text!Here is Sample text!Here is Sample text!Here is Sample text!Here is Sample text!Here is Sample text!Here is...\" }"}

标签: mongodbexpressmongoosetext

解决方案


好像你有一个content字段索引。考虑删除此索引,或将其替换为文本索引

UPD:如果您的架构说,Mongoose 会自动创建唯一索引unique: true - 只需为该content字段删除这部分即可。Mongoose 模式类型


推荐阅读