db.city.insertMany([
  { id: 1, names: "C, D, E"},
  { id: 2, names: "A, B"}
]);

// With using | delimiter
const match = ['A', 'E'];
const delimiterMatchResult = db.city.find({ names: { $regex: match.join("|"), $options: 'i' }});
console.log("\nResult:", delimiterMatchResult);

const match = ['C', 'K'];
const delimiterMatchResult2 = db.city.find({ names: { $regex: match.join("|"), $options: 'i' }});
console.log("\nResult2:", delimiterMatchResult2);

// With mapping Regex
const mapRegex = array => array.map(val => new RegExp(val, 'i'));

const firstMatch = ['A', 'B'];
const firstResult = db.city.find({ names: {$in: mapRegex(firstMatch) }});
console.log("\nFirst Result:", firstResult);

const secondMatch = ['C', 'D'];
const secondResult = db.city.find({ names: {$in: mapRegex(secondMatch) }});
console.log("\nSecond Result:", secondResult);

const thirdMatch = ['A', 'C'];
const thirdResult = db.city.find({ names: {$in: mapRegex(thirdMatch) }});
console.log("\nThird Result:", thirdResult);






Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: