Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Django MongoDB Backend
/

Model Geospatial Data

In this guide, you can learn how to use Django MongoDB Backend to store geospatial data. Django MongoDB Backend supports Django's GeoDjango web framework, which allows you to work with geospatial data in your Django applications.

Important

GeoDjango Limitations

Django MongoDB Backend's GeoDjango support has several limitations. To view a list of limitations, see Contrib App Support in the Django and MongoDB Feature Compatibility guide.

Tip

To use the examples in this guide, ensure that you have a Django application that uses Django MongoDB Backend and accesses the Atlas sample datasets. To learn how to set up your application, see the Get Started tutorial.

Before you can use Django MongoDB Backend to model geospatial data, you must configure your Django project to use GeoDjango.

To use GeoDjango, perform the following steps:

  1. Install the GEOS and GDAL geospatial libraries.

  2. Navigate to your project's settings.py file and add the django.contrib.gis app to the INSTALLED_APPS setting, as shown in the following code:

    INSTALLED_APPS = [
    "django.contrib.gis",
    # ... Other installed apps
    ]

To store geospatial data in your models, add the following import statement to your project's models.py file:

from django.contrib.gis.db import models

Important

If you use the from django.db import models import statement in your models.py file, ensure that you replace it with the preceding import statement before defining GeoDjango fields.

Then, you can use GeoDjango fields to define geospatial fields in your model classes. MongoDB stores each field type as a GeoJSON object in your collection documents. Django MongoDB Backend automatically creates 2dsphere indexes on these fields, which allows you to perform geospatial queries.

Tip

To learn how to query geospatial data by using the raw_aggregate() method, see Query Geospatial Data in the Perform Raw Database Queries guide.

The following table lists the supported GeoDjango fields and their corresponding MongoDB GeoJSON object types:

GeoDjango Field
GeoJSON Type

PointField

LineStringField

PolygonField

MultiPointField

MultiLineStringField

MultiPolygonField

GeometryCollectionField

Tip

To learn more about the GeoDjango fields, see Spatial Field Types in the Django documentation.

This example defines Theater and Geo models to represent the sample_mflix.theaters collection from the Atlas sample datasets.

In MongoDB, this sample collection's location field stores a Point object that represents the theater's geographic location. Each field stores the following data:

{
"location": {
"address": {
"street": "<street name>",
"city": "<city name>",
"state": "<state name>",
"zip": "<zip code>"
},
"geo": {
"type": "Point",
"coordinates": [<longitude>, <latitude>]
}
}
}

You can use Django MongoDB Backend to store this data by creating the following models in your project's models.py file:

from django.contrib.gis.db import models
from django_mongodb_backend.models import EmbeddedModel
from django_mongodb_backend.fields import EmbeddedModelField
class Location(EmbeddedModel):
address = models.JSONField(null=True)
geo = models.PointField()
class Theater(models.Model):
theater_id = models.IntegerField(default=0, db_column="theaterId")
location = EmbeddedModelField(Location, null=True, blank=True)
class Meta:
db_table = "theaters"
managed = False
def __str__(self):
return self.theater_id

In the preceding code, the Theater model has a location field that stores an embedded Location model. The embedded model contains a PointField named geo that stores location data.

To learn more about GeoDjango models, see GeoDjango Model API in the Django documentation.

To learn more about storing geospatial data in MongoDB, see Geospatial Data in the MongoDB Server documentation.

Back

Create Models

On this page