$text 查询所需的文本索引 [英] text index required for $text query
本文介绍了$text 查询所需的文本索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个错误$text 查询需要文本索引".但正如我所见,我所做的一切都是正确的.
I have an error "text index required for $text query". But as I see i made all correctly.
const JobSchema = mongoose.Schema({
jobTitle: {
type: String,
required: true,
text: true,
index: true
}
});
JobSchema.index({ jobTitle: 'text'});
router.get('/search', async (req, res) => {
try {
const searchResults = await Job.find({$text: {$search: req.query.q }})
console.log(searchResults);
res.json({
searchResults: searchResults
});
} catch (err) {
res.json({message: err});
}
});
任何帮助将不胜感激:)
Any help would be appreciated :)
推荐答案
Add mongoose.set('useCreateIndex', true);
在你的入口点文件(例如 app.js)上需要猫鼬.例如
Add mongoose.set('useCreateIndex', true);
on your entry point file (app.js for example) after you required mongoose.
e.g.
var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
这篇关于$text 查询所需的文本索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文