未创建索引,$text 查询需要文本索引 - mongoose

Cha*_*wki 9 indexing search mongoose mongodb node.js

mongoose: 5.8.9
node: v12.13.0
Run Code Online (Sandbox Code Playgroud)

设置在架构上创建索引后,猫鼬不会创建索引,创建新文档后只有 ID 索引,没有新创建的内容。我已经按照文档中提到的创建索引的方式进行了操作,但我仍然无法弄清楚我在哪里犯了错误。

什么时候

 const ads = await Ad.find({ $text: { $search: "something" } })
Run Code Online (Sandbox Code Playgroud)

错误

MongoError: text index required for $text query
    at Connection.<anonymous> (/home/usama/Projects/commercial/adex/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:466:61)
    at Connection.emit (events.js:210:5)
    at Connection.EventEmitter.emit (domain.js:476:20)
    at processMessage (/home/usama/Projects/commercial/adex/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:384:10)
    at Socket.<anonymous> (/home/usama/Projects/commercial/adex/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:553:15)
    at Socket.emit (events.js:210:5)
    at Socket.EventEmitter.emit (domain.js:476:20)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  ok: 0,
  errmsg: 'text index required for $text query',
  code: 27,
  codeName: 'IndexNotFound',
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {}
}
Run Code Online (Sandbox Code Playgroud)

我的架构

import { Schema } from 'mongoose'
import mongoosePaginate from 'mongoose-paginate-v2'
import Local from '../index'

const adSchema = new Schema(
  {
    id: Schema.Types.ObjectId,
    creater: Schema.Types.ObjectId,
    title: { type: String },
    tags: Array,
    description: { type: String, maxlength: 4500, },
  },
  {
    timestamps: true,
    versionKey: false,
    autoIndex: false
  }
)

adSchema.index({ title: 'text', description: 'text', tags: 'text' })
adSchema.plugin(mongoosePaginate)

const Ad = Local.model('Ad', adSchema)

export { Ad as default }
Run Code Online (Sandbox Code Playgroud)

在蒙戈外壳上

> myads.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "adex.ads"
    }
]

Run Code Online (Sandbox Code Playgroud)

mic*_*ckl 8

下面一行:

adSchema.index({ title: 'text', description: 'text', tags: 'text' })
Run Code Online (Sandbox Code Playgroud)

正确定义猫鼬模式(而不是数据库)的索引。默认情况下,猫鼬会在应用程序启动时创建索引(链接),但是您可以通过使用来阻止它autoIndex: false。

因此,您必须删除该行或createIndexes显式在您的模型上运行:

adSchema.index({ title: 'text', description: 'text', tags: 'text' });
const Ad = Local.model('Ad', adSchema);
Ad.createIndexes();
Run Code Online (Sandbox Code Playgroud)

  • 如果您想完全控制该过程或保留 Mongoose 的自动索引,您可以运行一次性脚本并创建所有索引 - 这实际上取决于您和您的组织的部署策略 (2认同)

Saj*_*thP 5

这是使用 MongoDB Compass 应用程序而不是使用 mongo 控制台解决相同问题的方法。

使用这样的连接字符串连接到数据库

mongodb://<user>:<password>@<ip>:<port>/<your db>?authSource=<user>&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false
Run Code Online (Sandbox Code Playgroud)

collection然后从左侧窗格中选择正确的选项,然后选择Indexes选项卡并创建文本索引,如下所示

在此输入图像描述