You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose报错unknown operator: $text问题求助

Mongoose报错unknown operator: $text问题求助

各位好,我遇到一个很奇怪的问题,想请教下大家:

我用Mongoose定义了一个Freelancer的Schema,并且给title和presentationText字段创建了文本索引,代码如下:

FreelancerSchema.index({ title: 'text', presentationText: 'text' }, { weights: { title: 2, presentationText: 1 } });

之后我写了全文搜索的逻辑,通过$text操作符来匹配搜索关键词:

const query = { $text: { $search: req.query.query } };
const freelancers = await Freelancer.find(query).select({ image: 1, firstName: 1, lastName: 1, title: 1, presentationText: 1, hourlyRate: 1, skills: 1 });

可是每次执行这段代码都会抛出错误:unknown operator: $text。但让我困惑的是,完全一样的查询语句在Mongo Shell里是可以正常执行并返回结果的,有没有人能帮我分析下这是哪里出了问题?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 09:34:38