目前,我在object中引用文本索引时遇到问题,代码如下
模式
var UserSchema = new mongoose.Schema({
username: String,
fullname: String,
email: {
type: String,
lowercase: true,
unique: true
},
supplier: Boolean,
supplierdetails: {
name: String,
businesstype: String,
location: String,
products: String,
revenue: String,
employees: String,
yearsestablished: String
}
});
UserSchema.index({supplierdetails: 'text'});
module.exports = mongoose.model('User', UserSchema);API接口
router.post('/findsupplier', function(req, res){
User.find({supplier: true, $text: {$search: req.body.supplyData}}, {score: {$meta: 'textScore'}})
.sort({score: {$meta: 'textScore'}})
.exec(function(err, supplyResult){
if(err)
throw err;
else
res.json(supplyResult);
});
});正如您在这里看到的," supplierdetails“是我的模式上的一个对象,我告诉mongoosejs对它进行文本索引,因为我想对包含名称、商业类型、产品、位置等的整个supplierdetails对象进行文本索引搜索。

但是它仍然不工作这是我在数据库中的数据

我搜索了"dog“或"shiba”,但仍然没有返回任何结果。
我的另一个使用文本索引的功能工作得很好,唯一的区别是,我的文本索引不是一个对象,它只是一个字符串属性,它工作得很好
我做错了吗?
发布于 2017-07-25 00:27:00
根据documentation,您只能在字符串或字符串数组上创建文本索引,而不能在对象(恰好包含字符串)上创建文本索引。
https://stackoverflow.com/questions/43999777
复制相似问题