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

问题描述 投票:1回答:1
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 shell上]

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

mongoose:5.8.9节点:v12.13.0设置为在架构上创建索引之后,mongoose不会创建索引。在创建新文档之后,只有ID索引没有新创建的内容。我有...

node.js mongodb search mongoose indexing
1个回答
0
投票

下一行:

© www.soinside.com 2019 - 2024. All rights reserved.