Mongoosejs文本索引一个对象

时间:2017-05-16 11:07:30

标签: node.js mongodb mongoose full-text-indexing

目前我有关于在对象中引用文本索引的问题 这是代码

模式

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对象进行文本索引搜索,该对象包含name,businesstype,products,location等等。我在我的mongo shell上看到了创建的索引 enter image description here

但它仍然没有用 这是我的数据库数据

enter image description here

我搜索了“狗”或“shiba”,但仍然没有给我留下任何结果。

我使用文本索引的其他功能完全正常,唯一的区别是,我的文本索引不是一个对象,它只是一个String属性,它完美地工作

我做错了吗?

1 个答案:

答案 0 :(得分:0)

根据documentation,您只能在字符串或字符串数​​组上创建文本索引,而不是对象(恰好包含字符串)。

相关问题