【发布时间】: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