(GDAL/OGR >= 2.1.0)

This driver can connect to the a MongoDB service.

The driver supports read, creation, update and delete operations of documents/features and collections/layers. The MongoDB database must exist before operating on it with OGR.

MongoDB vs OGR concepts

A MongoDB collection within a database is considered as a OGR layer. A MongoDB document is considered as a OGR feature.

Dataset name syntax

There are two main possible syntaxes: The open options available are : Note: the SSL_* and FIPS_MODE options must be set to the same values when opening multiple types MongoDB databases. This is a limitation of the Mongo C++ driver.


The driver will forward any spatial filter set with SetSpatialFilter() to the server when a "2d" or "2dsphere" spatial index is available on the geometry field.

However, in the current state, SQL attribute filters set with SetAttributeFilter() are evaluated only on client-side. To enable server-side filtering, the string passed to SetAttributeFilter() must be a JSon object in the MongoDB filter syntax.


Features are retrieved from the server by chunks of 101 documents or just enough documents to exceed 1 megabyte. Subsequent batch size is 4 megabytes. This can be altered with the BATCH_SIZE open option.


When reading a MongoDB collection, OGR must establish the schema of attribute and geometry fields, since, contrary to MongoDB collections which are schema-less, OGR has a fixed schema concept.

In the general case, OGR will read the first 100 documents (can be altered with the FEATURE_COUNT_TO_ESTABLISH_FEATURE_DEFN open option) of the collection and build the schema that best fit to the found fields and values.

If the collection/layer has been previously created with OGR, a _ogr_metadata special collection contains the OGR schema, in which case it will be directly used. It might be possible to ignore the schema written in _ogr_metadata by setting the USE_OGR_METADATA=NO open option.

It is also possible to set the JSON_FIELD=YES open option so that a _json special field is added to the OGR schema. When reading MongoDB documents as OGR features, the full JSon version of the document will be stored in the _json field. This might be useful in case of complex documents or with data types that do not translate well in OGR data types. On creation/update of documents, if the _json field is present and set, its content will be used directly (other fields will be ignored).

Feature ID

MongoDB have a special _id field that contains the unique ID of the document. This field is returned as an OGR field, but cannot be used as the OGR special FeatureID field, which must be of integer type. By default, OGR will try to read a potential 'ogc_fid' field to set the OGR FeatureID. The name of this field to look up can be set with the FID open option. If the field is not found, the FID returned by OGR will be a sequential number starting at 1, but it is not guaranteed to be stable at all.

ExecuteSQL() interface

If specifying "MongoDB" as the dialect of ExecuteSQL(), a JSon string with a serialized MongoDB command can be passed. The result will be returned as a JSon string in a single OGR feature.

Standard SQL requests will be executed on client-side.

Write support

Layer/collection creation and deletion is possible.

Write support is only enabled when the datasource is opened in update mode.

When inserting a new feature with CreateFeature(), and if the command is successful, OGR will fetch the returned _id and use it for the SetFeature() operation.

Layer creation options

The following layer creation options are supported:


  • Listing the tables of a MongoDB database:
    ogrinfo -ro mongodb://

  • Filtering on a MongoDB field:
    ogrinfo -ro mongodb:// -where '{ "field": 5 }'

  • Creating and populating a collection from a shapefile:
    ogr2ogr -update mongodb:// shapefile.shp

    Build instructions

    GDAL/OGR must be built against the MongoDB C++ driver client library, in its "legacy" version (tested with 1.0.2), in order to the MongoDB driver to be compiled.

    You must first follow MongoDB C++ driver client build instructions, which require to have Boost libraries available.


    See Also