I have a model as shown
[
{"_id": "1", "Title": "boat test"},
{"_id": "2", "Title": "mobiles and extras"}
]
Query_String = "boat" // should return [{"_id": "1", "Title": "boat test"}]
Query_String = "boat te" // should return [{"_id": "1", "Title": "boat test"}]
Query_String = "mobiles" // should return [{"_id": "2", "Title": "mobiles and extras"}]
Query_String = "mob" // should return [{"_id": "2", "Title": "mobiles and extras"}]
Query_String = "boat mobile" // should return [{"_id": "1", "Title": "boat test"}, {"_id": "2", "Title": "mobiles and extras"}]
My query is to get all products whose "Title" field has any of the words in a given Query_String
I tried this but this works for 1st case only in which Query_String is "boat"
const { Products } = require("../models/Products");
Products.find({
Title: {
$regex: `.*${Query_String}.*`,
$options: "i",
},
})