我在现有的猫鼬集合中创建了索引。它在其他领域很少的收藏中工作得很好。但是它不能在具有引用objectid等的特定集合中工作。
const SolutionSchema = mongoose.Schema({
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, 'No User id found']
},
title: {
type: String
},
slug: {
type: String
},
description: {
type: String
},
categoryid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category',
required: [true, 'No Category id found']
},
});
SolutionSchema.index({
title: 'text'
});
const Solution = module.exports = mongoose.model('Solution', SolutionSchema);
路线
Solution.find({
$text: { $search: term },
})
.then(products => console.log(products))
.catch(e => console.error(e));
其显示此错误
{ MongoError: text index required for $text query
at Connection.<anonymous> (D:\projects\uow\node_modules\mongodb-core\lib\connection\pool.js:443:61)
at Connection.emit (events.js:198:13)
at processMessage (D:\projects\uow\node_modules\mongodb-core\lib\connection\connection.js:364:10)
at Socket.<anonymous> (D:\projects\uow\node_modules\mongodb-core\lib\connection\connection.js:533:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
ok: 0,
errmsg: 'text index required for $text query',
code: 27,
codeName: 'IndexNotFound',