# Exercises

## Exercise 1

* Create a geospatial index (2dsphere) for the states collection
  * Get information on states collection’s indexes

    ```python
    {'_id_': {'key': [('_id', 1)], 'ns': 'geo.states', 'v': 2}, 
    'loc_2dsphere': 
    {'2dsphereIndexVersion': 3, 'key': [('loc', '2dsphere')], 'ns': 'geo.states', 'v': 2}}
    ```
* Remove the geospatial index from the states collection
  * Get information on states collection’s indexes

    ```python
    {'_id_': {'v': 2, 'key': [('_id', 1)], 'ns': ‘geo.states'}}
    ```

## Exercise 2

* Use the restaurant collection in the geo database

1. Create a valid query and print the query plan using executionStats•Find documents between '10200‘ and '10280‘ in zipcode
2. Create an index to zipcode (Hint: Embedded Document)
3. Execute #1, again.

* &#x20;No Index

  ```python
    "executionStats" : {
   …
                  "executionTimeMillis" : 11,
                  "totalKeysExamined" : 0,
                  "totalDocsExamined" : 25359,
                  "nReturned": 21,
  …
  ```

* Indexed

  ```python
   "executionStats" : {
  …
                  "executionTimeMillis" : 0,
                  "totalKeysExamined" : 21,
                  "totalDocsExamined" : 21,
                  "nReturned": 21,
  …
  ```

* Use the restaurant collection in the geo database

1. Create a query and print the query plan using executionStats•Find documents that have at least the grade is ‘B’ in the grades.grade field.
2. Create an index to grade (Hint: Indexing on Array)
3. Execute #1, again.

* No Index

  ```python
   "executionStats" : {
  …
                  "executionTimeMillis" : 17,
                  "totalKeysExamined" : 0,
                  "totalDocsExamined" : 25359,
                  "nReturned": 8280,
  …
  ```
* Indexed

  ```python
   "executionStats" : {
  …
                  "executionTimeMillis" : 13,
                  "totalKeysExamined" : 8280,
                  "totalDocsExamined" : 8280,
                  "nReturned": 8280,
  …
  ```

## Exercise 3

* Find international airports in the state that has the above zip code (10044)
  * Sorting the name in ascending order&#x20;
  * Print state’s name and airport name each.

    * Hint: states and small\_zip collections

    ```python
    [{'name': 'Albany County'}, 
    {'name': 'Greater Buffalo Intl'}, 
    {'name': 'Greater Rochester International'}, 
    {'name': 'John F Kennedy Intl'}, 
    {'name': 'La Guardia'}, 
    {'name': 'Massena Intl'}, 
    {'name': 'Niagara Falls Intl'}, 
    {'name': 'Ogdensburg Intl'}, 
    {'name': 'Stewart Intl'}, 
    {'name': 'Syracuse Hancock Intl'}, 
    {'name': 'Watertown International'}] 
    ```
* Find Korean restaurants that are located at less 2km from international airports.
  * Warn: should create 2dsphere index to address for restaurants collection.

![](/files/-LdoIPpZFkXDmZvkkOms)

* Find states that are intersect to the flight path (trajectory).&#x20;
  * Print names including source and destination states.&#x20;

![](/files/-LdoImMimuG7M5dZS3GK)

* Make Geojson format whose type is LineString.&#x20;

  * Find the two coordinates (LAX, DTW) Result

* Result

  ```python
  [{'name': 'Arizona'},
  {'name': 'California'},
  {'name': 'Colorado'},
  {'name': 'Illinois'},
  {'name': 'Iowa'},
  {'name': 'Kansas'},
  {'name': 'Michigan'},
  {'name': 'Nebraska'}, 
  'name': 'Nevada'},
  {'name': 'Utah'}]
  ```

## Exercise 4

* Using the restaurants collection.

* Find restaurants whose names include "Kimchi".&#x20;

* Print only name, zipcodeand address

  ```python
  { "zipcode" : 11231, "address" : "Smith Street 478" }
  { "zipcode" : 11238, "address" : "Washington Avenue 766" }
  { "zipcode" : 10036, "address" : "West 48 Street 18-20" }
  ```

* Using the restaurants and states collections.

* Find restaurants that have "NolbuRestaurant＂(restaurants collection)&#x20;

* Print the state (states collection)

  ```python
  New York
  ```

* Using restaurants.json&#x20;

* Among the "Korean" restaurants, find a restaurant that has "Ramen"but "Izakaya“.&#x20;

* Then, find a restaurant near above restaurant in 150m.

  ```python
  { '_id' : ObjectId('5c8626739f44c9f6768035da'),
    'address' : {'building' : '8', 'coord': [-73.9916305, 40.7247106],
                 'street': 'Extra Pl', 'zipcode': '10003'},
    'borough': 'Manhattan',
    'cuisine': 'Japanese',
    'grades': [{'date': datetime.datetime(2015, 1, 20, 0, 0),
                 'grade': 'Not Yet Graded',
                 'score': 21}],
    'name': 'Ko Ep, Llc',
    'restaurant_id': '50015854'}
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://koonkim.gitbook.io/mongodb/master/index/advanced-index-in-pymongo/exercises.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
