How to Migrate MongoDB Geodatabase to ApsaraDB for MongoDB

By Grace Amondi, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

A geodatabase is a database that is optimized for storing and querying data that represents objects defined in a geometric space. Most spatial databases allow the representation of simple geometric objects such as points, lines, and polygons. Some spatial databases handle more complex structures such as 3D objects, topological coverage, linear networks, and TINs. MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling. MongoDB supports a rich query language to support geospatial queries as we will see in this tutorial.

In this tutorial, we are going to go through a step by step process of migrating a local MongoDB Geodatabase to ApsaraDB for MongoDB on Alibaba Cloud.

MongoDB Geospatial Support

MongoDB is well known to support the storage, querying, transmission and retrieval of geospatial data in the form of GeoJSON objects or as legacy coordinate pairs. This is helpful in calculating geometry over an Earth-like sphere.

To specify GeoJSON data, use an embedded document with:

  • a field named type that specifies the GeoJSON object type and
  • a field named coordinates that specifies the object’s coordinates.
  • If specifying latitude and longitude coordinates, list the longitude first and then latitude:
  • Valid longitude values are between -180 and 180, both inclusive.
  • Valid latitude values are between -90 and 90 (both inclusive).

Why MongoDB for Geospatial Querying?

  1. MongoDB performs better as the spatial data size increases.
  2. Spatial Data retrieval is also generally faster with MongoDB and is often easier to structure within existing code for programs, not requiring complex SQL queries that could be less efficient for given data problems.
  3. It facilitates analysis and integration within a variety of tools, which is why open source GIS has proven to be the most useful arena for NoSQL databases.
  4. MongoDB, which is a NoSQL database that is open source and can be integrated with many open source tools such as QGIS.
  5. It delivers fast output for geospatial queries.


For this tutorial, you will need to have:
1. A valid Alibaba Cloud account. If you don’t have one already, sign up to the Free Trial to enjoy up to $300 worth in Alibaba Cloud products.
2. An ECS instance running Ubuntu 16.04. You can select your preferred region and configurations; this will not affect the outcome of the server setup.
3. A root password for your server.
4. An ApsaraDB for MongoDB instance.

Step 1: MongoDB Installation

We are going to need to first install MongoDB on our local instance.

Add the MongoDB repository:

Create a list file for MongoDB:

Update package list:

Install MongoDB Package:

Next start MongoDB using systemctl:

You can check the service status using:

If successful you should have something similar to this:

Finally, enable MongoDB to automatically start when the system starts:

Step 2: Create a New Database

MongoDB use DATABASE_NAME is used to create a new database or connect to an already existing database:

First, initialize mongo shell using the command:

Next, create a database using the command:

This will create a database by the name mongospatial. You can check if it’s in the list of databases by typing:

Step 3: Populate MongoDB Database

In this section, we are going to simply be storing geospatial data as GeoJSON objects in our mongospatial database. MongoDB uses WGS84 reference system for both storage and geospatial queries on geoJSON objects.

We are going to create a collection named places which will contain several place documents. Copy and paste the following into the mongo shell.

MongoDB supports other GeoJSON objects such as :

  • Point -the “coordinates” member is a single position.
  • LineString — the “coordinates” member is an array of two or
    more positions.
  • Polygon — the “coordinates” member MUST be an array of
    linear ring coordinate arrays.
  • MultiPoint — the “coordinates” member is an array of
  • MultiLineString — the “coordinates” member is an array of
    LineString coordinate arrays.
  • MultiPolygon — the “coordinates” member is an array of
    Polygon coordinate arrays.
  • GeometryCollection — has a member with the name “geometries”. The
    value of “geometries” is an array.

Step 4: Setup Geospatial Indexes

In simple terms, indexing is the process of creating an index for a database structure to help expedite retrieval of data. Therefore geospatial indexing supports geospatial queries. MongoDB provides the following index types :

  1. 2d sphere
  2. 2d

In our case, we will be using 2d sphere which supports queries that calculate geometries on an Earth-like sphere. So go ahead and run the following command in mongo shell:

Let’s check if our places collection was successfully populated:

You should have something like this:

Step 5: Perform Geospatial Queries

MongoDB provides a number of geospatial query operators.

  • $geoIntersects — it selects geometries that intersect with a GeoJSON geometry.
  • $geoWithin -it selects geometries within a bounding GeoJSON geometry.
  • $near -It returns geospatial objects in proximity to a point.
  • $nearSphere — it returns geospatial objects in proximity to a point on a sphere.

Let’s try out $near operator for this tutorial. It should return geospatial objects in proximity to a point. We will use it to return documents at least 1000meters from and at most 5000meters from the specified GeoJSON point, sorted from the nearest to the farthest.

Type the following command in mongo shell:

Step 6: Creating a Backup File

MongoDB allows us to back up our local database into a file using the command mongodump

You should see the following output:

Step 7: Migrate the Geospatial Data to ApsaraDB for MongoDB

Access your already created instance using ssh command:

Install MongoDB on the ECS instance using the same steps used in Step 1 of this tutorial.

Next access ApsaraDB for MongoDB instance using:

Next, you are going to copy the dump file using scp to your ECS Instance.

Finally, import the backup dump file using mongorestore command:

Step 8: Check if the data arrived

Initialize mongo shell on ECS instance and check if the database, collection and documents were successfully migrate:

You should see the following:

You can also try and perform geospatial queries to ascertain that everything works fine.


In summary, we were able to install MongoDB on our local instance, create a mMngoDB database, populate the MongoDB database with geospatial data, create geospatial indexes and perform geospatial queries. We were also able to migrate the geospatial data from our local MongoDB dataabase to ApsaraDB RDS for MongoDB on Alibaba Cloud.

You can go ahead and perform other geospatial queries such as $geowithin and see how far you can go.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store