2

Suppose I want to query all the place_name which are in United State. What should be my query or fields?

Here i have pasted my query down which gives me only first index but I want to get all the data. What should be my query then.

{
      "post_code": 35801,
      "country": "United States",
      "country_abbreviation": "US",
      "places": [
        {
          "place_name": "Huntsville",
          "longitude": -86.5673,
          "state": "Alabama",
          "state_abbreviation": "AL",
          "latitude": 34.7269
        },
{
          "place_name": "Huntsville12345",
          "longitude": -86.5673,
          "state": "Alabama",
          "state_abbreviation": "AL",
          "latitude": 34.7269
        }
      ]
    },

Present query is

"selector": {
    "places.0.place_name": {
        "$gte": null
      }

  },
  "fields": [
    "_id",
    "places.0.place_name"
  ],
  "sort": [
    {
      "places.0.place_name": "asc"
    }
  ]
}
Alexis Côté
  • 3,670
  • 2
  • 14
  • 30

1 Answers1

2

For this kind of query, you should use views.

You could create a view that map the following key: [country,place_name]

Map function eg:

function(doc){
  if(doc.places){
    for(var i in doc.places){
      emit([doc.country,doc.places[i].place_name);
    }
  }
}

This let you query: the list of countries and the list of places per country.

If you want the list of countries, you can use the following parameters in your query: group_level=1&group=true

If you want to get the places of the united states: startkey=['United states']

Alexis Côté
  • 3,670
  • 2
  • 14
  • 30