0

I have a collection as follows, enter image description here

I want to get the cities for the specific country. what should be the query?

This is what i have tried.

db.getCollection('_event').find([
 {$match: {}},
 {$project: {
  cities: {
   $filter: {
    input: 'city',
    as: 'r',
    cond: {$eq: ['$$r.country', 'Portugal']}
   }
  }
 }}
])
  • Possible duplicate of [Mongoose, Select a specific field with find](http://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) – ippi May 03 '17 at 05:50
  • Images are useful in a post, but **make sure the post is still clear without them**. Instead of showing a screenshot of your data format, copy and paste or type the actual data into the post directly. cf http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors – Vince Bowdren Jun 15 '17 at 11:59

2 Answers2

1

Please use as follow

db.getCollection('_event').find(
    {'location.country':'Portugal'},{'location.city':1}
    )
Love-Kesh
  • 777
  • 7
  • 17
0

The way you have tried is the Pipeline way which is implement with Aggregation (For modification use aggregation)

db.getCollection('_event').aggregation([
 {$match: {}},
 {$project: {
  cities: {
   $filter: {
    input: 'city',
    as: 'r',
    cond: {$eq: ['$$r.country', 'Portugal']}
   }
  }
 }}
])

or in simple way (to get whole models):-

 1   db.getCollection('_event').find({"location.country":"Portugal"})
 2   db.getCollection('_event').find({"location.country":\Portugal\}) 

2nd is for same like Portugal

Thanks.

hardy
  • 880
  • 2
  • 13
  • 30