简体   繁体   中英

Full text search with mongoose not working

I am trying to search an object using text as a parameter.

I have the following data in my mongodb:

[
    {
        "_id": "59d518859eacefa4555d6edb",
        "id": "A07",
        "nome": "Outras doenças intestinais por protozoários"
    },
    {
        "_id": "59d518859eacefa4555d6edc",
        "id": "A08",
        "nome": "Infecções intestinais virais, outras e as não especificadas"
    }
]

My model:

var cid = new Schema({
    id: String,
    nome: String
}, {collection: 'cid'});

cid.index({'id': 'text', 'nome': 'text'});

Method to search:

var cid = require('./cid.model');

cid.find({$text: {$search: 'A07'}})
     .exec(function(err, result) {
       if (err) {
         console.log(err);
       } else {
         res.json(result)
       }
     })

When I use 'A07' as parameter works.

But when I use 'Outras doenças intestinais por protozoários' as a parameter or some words, such as: 'doenças', 'outras' does not return anything.

What am I letting go?

I would do this way:

cid.find({
        $or: [
            {name: new RegExp(your_variable, "i")},
            {id: new RegExp(your_variable, "i")}
        ]
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM