3

I have a msg.text variable Incomming from Telegram Bot, msg.text is = My Schema name, How Should I get names Contain words entered by the user ?

schema:

const parentSchema = new Schema({
    _id: Number,
    name: String,
});
parentSchema.plugin(mongoosastic, {
    hosts: [
        'localhost:9200'
    ]
});
const Mq = mongoose.model('Mq', parentSchema);

module.exports = Mq;

Code:

bot.onText(/\/search/, (msg) => {
// mangoosastic search Code
});

Forexample in my db I have {rock, book, pre rock, Cat, rock after, and pre rock after} and user msg.txt = ro How Should I get and console.log Words Contain roin node.js Project:

I need to get:
rock
pre rock
rock after
pre rock after

Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

1 Answers1

0

My Problem Solved With Native MongoDB regex

parentSchema.index({ name: 'text' });
    Mq.find(
        { "name": { "$regex": "ro", "$options": "i" } },
        function(err,docs) { 
        } 
    );
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103