2

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",
      },
    })
0

1 Answer 1

2

There are 2 approaches,

Using javascript RegExp:

  • split string by space and make array, map to convert string to regular express
  • Try $in Expressions, To include a regular expression in an $in query expression, you can only use JavaScript regular expression objects (i.e. /pattern/ ). For example:
let QString = Query_String.split(" ").map(s => new RegExp(s));
Products.find({
  $or: [
    { Title: { $in: QString } },
    // more fields if needed
    { Category: { $in: QString } }
  ]  
});

Using $or conditional and $regex

  • split string by space and make array, map to prepare array of separate conditions
  • check $or condition
let QStringTitle = Query_String.split(" ").map(s => { 
  return {
    {
      Title: {
        $regex: s,
        $options: "i"
      }
    }
  };
});
let QStringCategory = Query_String.split(" ").map(s => { 
  return {
    {
      Category: {
        $regex: s,
        $options: "i"
      }
    }
  };
});
Products.find({ $or: QStringTitle.concat(QStringCategory) });
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this out and it worked out prefectly fine... let TitleFilter = req.body.Title.split(" ").map((s) => { return { Title: { $regex: s, $options: "i", }, }; }); const product = await Products.find({ $or: TitleFilter }); // this returned output as expected but what if I want to also check Category Field for the same query.... For ex right now I am checking Just for Title field..what if I want to check for Title and Category field both at the same time...i.e; for Multiple fields.
I have updated the answer with category condition.
I used the second approach for my problem. It works Perfectly fine. Thanks! We can use the spread operator also here just to concat the two arars like this.. [...TitleFilter, ...CategoryFilter]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.