【问题标题】:Index is not getting created, text index required for $text query - mongoose未创建索引,$text 查询需要文本索引 - mongoose
【发布时间】:2020-05-12 05:08:46
【问题描述】:
mongoose: 5.8.9
node: v12.13.0

设置在模式上创建索引后,mongoose 不会创建索引,创建新文档后只有 ID 索引没有新创建。我已经按照creating index 提到的文档完成了操作,但我仍然无法弄清楚我在哪里犯了错误。

何时

 const ads = await Ad.find({ $text: { $search: "something" } })

错误

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)]: {}
}

我的架构

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 }

在 mongo 外壳上

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

【问题讨论】:

  • 在您的 My schema 中,Local 是什么?这个const Ad = Local.model('Ad', adSchema) 必须是import { Schema, mongoose } from 'mongoose'; const Ad = mongoose.model('Ad', adSchema)
  • 在我的服务器上,我运行两个单独的 MongoDB 数据库,本地运行存储数据在服务器远程存储上的 MongoDB 图集
  • :Mickl 是对的,你需要删除 autoIndex:false - 通常你会为 prod 服务器启用它,我已经观察到了..

标签: node.js mongodb search mongoose indexing


【解决方案1】:

下一行:

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

在 mongoose schema 上正确定义了索引(不在数据库上)。默认情况下,猫鼬会在您的应用程序启动时创建索引 (link),但是您可以使用 autoIndex: false 来阻止它。

因此,您必须删除该行或在您的模型上明确运行 createIndexes

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

【讨论】:

  • 非常感谢这创建了text 索引,我应该在 prod env 中做什么?我应该删除Ad.createIndexes(); 还是保留它。
  • 如果您想完全控制该过程或保持 Mongoose 的自动索引功能,您可以运行一次性脚本并创建所有索引 - 这实际上取决于您和您的组织拥有的部署策略跨度>
【解决方案2】:

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

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

mongodb://<user>:<password>@<ip>:<port>/<your db>?authSource=<user>&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false

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

【讨论】:

    【解决方案3】:

    对我来说,没有警告的最佳方法是来自 mongodb 可以

    你需要通过代码:>>

    const blogPosts = await BlogPostModel.find({  $text: { $search: `"${req.query["your-search-query"] }"`  } })
    

    【讨论】:

      猜你喜欢
      • 2020-09-07
      • 2019-08-16
      • 2020-03-25
      • 2023-03-26
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多