esty
esty

Reputation: 73

Return all documents containing a certain field with mongoose

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

Upvotes: 2

Views: 907

Answers (1)

mickl
mickl

Reputation: 49945

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);

Upvotes: 3

Related Questions