猫鼬不创建文本索引
Mongoose does not create text index
我有以下架构:
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);
问题是 mongoose 不创建文本索引。不过,标签和作者的索引已正确创建。
我是否以错误的方式使用了 index()
功能?
我在 mongod
会话中没有收到任何错误。它记录了非文本索引的成功索引创建,但似乎猫鼬从未为文本索引调用 ensureIndex
。
按照 Mongoose Not Creating Indexes 中所述进行调试后(感谢@JohnyHK link),我发现实际问题不是文本索引。
我正在使用 mongoose-auto-increment 插件,这导致索引 _id
字段时出错。
解决方案是让 autoIncrement 不使用 _id
字段,而是使用像这样的单独字段:
entrySchema.plugin autoIncrement.plugin, {
model: 'Entry'
startAt: 1000
field: 'shortId'
}
我只是没有考虑,因为索引在没有文本索引的情况下工作正常。插件和文本索引似乎存在某种不兼容问题。
我有以下架构:
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);
问题是 mongoose 不创建文本索引。不过,标签和作者的索引已正确创建。
我是否以错误的方式使用了 index()
功能?
我在 mongod
会话中没有收到任何错误。它记录了非文本索引的成功索引创建,但似乎猫鼬从未为文本索引调用 ensureIndex
。
按照 Mongoose Not Creating Indexes 中所述进行调试后(感谢@JohnyHK link),我发现实际问题不是文本索引。
我正在使用 mongoose-auto-increment 插件,这导致索引 _id
字段时出错。
解决方案是让 autoIncrement 不使用 _id
字段,而是使用像这样的单独字段:
entrySchema.plugin autoIncrement.plugin, {
model: 'Entry'
startAt: 1000
field: 'shortId'
}
我只是没有考虑,因为索引在没有文本索引的情况下工作正常。插件和文本索引似乎存在某种不兼容问题。