【问题标题】:Mongoose does not create text indexMongoose 不创建文本索引
【发布时间】:2023-03-26 00:43:01
【问题描述】:

我有以下架构:

entrySchema = new mongoose.Schema({

    size:    { type: Number },

    title:   {type: String, trim: true },
    content: { type: String, trim: true },

    tags:    { type: [String], trim: true, index: true },
    author:  { type: String, trim: true, index: true }
  });


entrySchema.index({ title: "text", content: "text" });

module.exports = mongoose.model('Entry', entrySchema);

问题是猫鼬不创建文本索引。不过,标签和作者的索引是正确创建的。

我是否以错误的方式使用了index() 函数?

mongod 会话中没有任何错误。它记录了非文本索引的成功创建索引,但似乎 mongoose 从未为文本索引调用 ensureIndex

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

按照Mongoose Not Creating Indexes(感谢@JohnyHK 提供链接)中所述进行调试后,我发现实际问题不是文本索引。

我使用的是mongoose-auto-increment 插件,导致索引_id 字段时出错。 解决方案是让 autoIncrement 不使用 _id 字段,而是使用这样的单独字段:

entrySchema.plugin autoIncrement.plugin, {
    model: 'Entry'
    startAt: 1000
    field: 'shortId'
}

我只是没有考虑这一点,因为没有文本索引的索引工作正常。插件和文本索引似乎存在某种不兼容。

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 2019-12-18
    • 2015-01-09
    • 2020-05-12
    • 2015-03-17
    • 2021-10-25
    • 2022-10-30
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多