Docs Menu

Docs HomeMongoDB Atlas

How to Run a Compound Geo JSON Query

On this page

  • Create the Atlas Search Index
  • Run a Combined Geo, Number, and Text Fields Query

This tutorial describes how to create an index on the listingsAndReviews collection in the sample_airbnb database and run a query that returns documents with the name, address, and property_type for each property within the specified polygon defined using coordinates.

This tutorial takes you through the following steps:

  1. Set up an Atlas Search index on the address field in the sample_airbnb.listingsAndReviews collection.

  2. Run a query that returns 10 documents with the name, address, and property_type of each property within the specified geographic coordinates. Atlas Search results reflect a preference for properties of type condominium, and each document in the result is assigned a relevance score, returned in order from highest to lowest.

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.

In this section, you will create an Atlas Search index on the address field in the sample_airbnb.listingsAndReviews collection.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster's name.

  4. Click the Search tab.

2
3
  • For a guided experience, select Visual Editor.

  • To edit the raw index definition, select JSON Editor.

4
  1. In the Index Name field, enter default.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_airbnb database, and select the listingsAndReviews collection.

5

You can use the Visual Editor or the JSON Editor in the Atlas user interface to create the index.

  1. Click Refine Your Index.

  2. In the Field Mappings section, click Add Field.

  3. Select address.location from the Field Name dropdown.

  4. In the Data Type Configuration section, click Add Data Type, and select Geo from the dropdown.

  5. Click Save Changes.

  1. Replace the default index definition with the following example index definition and click Next.

    {
    "mappings": {
    "dynamic": true,
    "fields": {
    "address": {
    "fields": {
    "location": {
    "type": "geo"
    }
    },
    "type": "document"
    }
    }
    }
    }

    The index definition above specifies that:

    • All of the fields in the collection must be automatically indexed.

    • The address.location field of a document must be indexed as type geo.

6
7

A modal window appears to let you know your index is building. Click the Close button.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


Use the Select your language drop-down menu on this page to set the language of the examples in this section.


In this section, you will run a query that returns 10 documents with the name, address, and property_type for each property within the specified geographic coordinates. A field specifying each documents score is also returned, and results are ordered with a preference for properties of type condominium.

←  How to Run Atlas Search Queries with a Date Range FilterHow to Use Facets with Atlas Search →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.