Return all documents containing a certain field with mongoose

Viewed 163

My schema has a string field companyName in my document. I get an object from the query string as {companyName:"Amazon,Microsoft"}}. How do I get back all documents that have company name either Amazon or Microsoft?

Right now I am changing the object to {companyName:{$regex:"Amazon Microsoft",$options:'i'}} and passing it to find() method but it is returning an empty array

1 Answers

You can transform your query string into $in operator:

let query = {companyName:"Amazon,Microsoft"};

let mongoDbQuery = { companyName: { $in: query.companyName.split(',') } }

let result = await Model.find(mongoDbQuery);
Related