【发布时间】:2020-06-20 21:00:51
【问题描述】:
我在我的项目中使用 mongodb 和 mongoose。我正在尝试使用文本索引,以便实现搜索功能。我已将 auotIndex 选项设置为 true,但我仍然收到以下错误“MongoError: text index required for $text query”,这表明尚未生成文本索引,因此如果我没记错的话 autoIndex 不起作用。
顺便说一句,我知道 autoIndex 不应该用于生产就绪的应用程序,因为它很重,但我正在进行的项目只是为了学习目的。
// Database start upp
(async () => {
const DB_URI = "mongodb://localhost:27017/photogram-db";
const DB_SETTINGS = {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
autoIndex: true
};
await mongoose.connect(DB_URI, DB_SETTINGS);
})();
// Person model / schema
const PersonSchema = new Schema({
firstName: String,
lastName: String,
username: String,
});
PersonSchema.index({
firstName: "text",
lastName: "text",
username: "text"
});
const PersonModel = model("Person", PersonSchema);
【问题讨论】: