Filter words in MongoDb in javaScript

Viewed 31

im building a chatbot to filter questions and i would like to know how to filter the search of my MongoDb with the input of the user.

By now I have this

I want to get all the results that match a word with a word of the variable words.

 let text = this.messageEvent.data.text

var words= text.split(" ")
this.fetchDataFromDataSource({ channel: this.channel, collectionName: "62a985781cd96396e4e1cba3_test", filter: {
   input:"$KeywordGroup1",
   
 } }).then((result) => {

            console.log(result)
  })
  

and my database looks like this database

i would like to filter with the input of the user. if a user writes price it should return the 3 entries of the database, but if the user writes any other thing it shouldn't return anything.

thanks

1 Answers

Is something with the elemMatch could help you?

db.collection.find({
  "data": {
    "$elemMatch": {
      "$eq": "price"
    }
  }
})

Find the playground here.

Related