MongoDB search from Array of objects

Viewed 20

I want to search cities from the countries selected, I am using the below find() query and its working perfectly for a single country. but how can i dot the same if I have multiple countries

var data = await model.find({city_contry: {$elemMatch: {name: 'Turkey'}}}, columns);

what I want to do is insted of "Turkey" I want to search from the array of object containing name of countries which are multiple,

I am sorry for the wrong english if i am using.

0: {code: '61c4a85712d5b30dd98820bc', name: 'United Arab Emirates'}
1: {code: '61c4a85712d5b30dd98820bd', name: 'Afghanistan'}
2: {code: '61c4a85812d5b30dd98820c0', name: 'Albania'}
3: {code: '61c4a85812d5b30dd98820bf', name: 'Anguilla'}
1 Answers

If I understand you correctly, you want to query the DB by passing multiple country names. You can use the $in operator of MongoDB and retrieve the data from different countries.

var data = await model.find({country: {$in: ["Austria", "Germany"]}});

For more information about comparison query operators, you can check the docs of MongoDB: Comparison Query Operators

Related