Filter on mongoDb an array of strings

Viewed 27

im building a chatbot and i want to filter a MongoDb database with the input of the users

words=["hello","price","bye"]

and my db is like that db

{
  "Lang": "en",
  "kw1": [
    "price"
  ],
  "Keyword Group 2": [
    "test"
  ],
  "Keyword Group 3": [
    "res"
  ],
  "Type": "Text",
  "Text": "aaaaaAAAAAaaa",
  "createdAt": 1662724328993,
  "etag": "d-Zwyn11c6q6DfK+AV6RVxl9i7OJQ",
  "_version": 2,
  "updatedAt": 1662724336488
}

i've tried to do it like this

this.fetchDataFromDataSource({ channel: this.channel, collectionName: "62a985781cd96396e4e1cba3_test", filter:{ kw1: words

 } }).then((result) => {
   
   console.log(result)

         
  }) 

when the input is only a word it works well but when i send more than one word it doesn't behave as expected, how im supposed to do it?

1 Answers

Try using the $in operator:

this.fetchDataFromDataSource({
  channel: this.channel,
  collectionName: '62a985781cd96396e4e1cba3_test',
  filter: { kw1: { $in: words } },
}).then((result) => {
  console.log(result);
});
Related