I am attempting to utilize MongoDB for performing searches based on multiple search strings.
Here is the format for executing a search with multiple strings in MongoDB:
db.meals.find({ $and: [ { mealName: /fish/ }, { mealName: /rice/ }, { mealName: /spicy/ } ] })
One challenge I encountered is that JavaScript objects cannot have duplicate keys within the same object.
I tried approaching the issue like this:
const str = "fish rice spicy";
const transform = (searchInput) => {
const searchField = {}
let searchArray = searchInput.split(" ");
searchArray = searchArray.map((item)=>{
const fixKey = "mealName";
searchField[fixKey] = new RegExp(item);
})
return searchField;
}
console.log(transform(str)); // {mealName: /spicy/}
Given this scenario,
How can I structure the MongoDB multiple searching string format in JavaScript?