【发布时间】: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。
【问题讨论】:
-
我认为你不能创建具有两个文本索引的复合索引:“复合文本索引不能包含任何其他特殊索引类型,例如多键或地理空间索引字段”(docs.mongodb.org/manual/core/index-text/#text-index-compound )
-
@joao 据我了解,这正是要走的路,如docs.mongodb.org/manual/tutorial/…中所述
-
你完全正确。
-
有关如何调试此问题的一些信息,请参阅此答案:stackoverflow.com/questions/12452865/…