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).
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }

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.

Prerequisites

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:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Create a list file for MongoDB:

$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Update package list:

$ sudo apt-get update

Install MongoDB Package:

$ sudo apt-get install -y mongodb-org

Next start MongoDB using systemctl:

$ sudo systemctl start mongod

You can check the service status using:

$ sudo systemctl status mongod

If successful you should have something similar to this:

● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: e
Active: active (running) since Thu 2018-10-25 00:50:30 EAT; 1h 34min ago
Docs: https://docs.mongodb.org/manual
Main PID: 1041 (mongod)
CGroup: /system.slice/mongod.service
└─1041 /usr/bin/mongod --config /etc/mongod.conf
Oct 25 00:50:30 geogirl systemd[1]: Started High-performance, schema-free document
lines 1-9/9 (END)

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

$ sudo systemctl enable mongod

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:

$ mongo

Next, create a database using the command:

use mongospatial

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

show dbs

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.

db.places.insert( {
name: "Central Park",
location: { type: "Point", coordinates: [ -73.97, 40.77 ] },
category: "Parks"
} );
db.places.insert( {
name: "Sara D. Roosevelt Park",
location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] },
category: "Parks"
} );
db.places.insert( {
name: "Polo Grounds",
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
category: "Stadiums"
} );

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
    positions.
  • 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:

db.collection.createIndex({location:"2dsphere")

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

db.places.find().pretty()

You should have something like this:

{
"_id" : ObjectId("5bd0d536ce045a817d917be6"),
"name" : "Central Park",
"location" : {
"type" : "Point",
"coordinates" : [
-73.97,
40.77
]
},
"category" : "Parks"
}
{
"_id" : ObjectId("5bd0d536ce045a817d917be7"),
"name" : "Sara D. Roosevelt Park",
"location" : {
"type" : "Point",
"coordinates" : [
-73.9928,
40.7193
]
},
"category" : "Parks"
}
{
"_id" : ObjectId("5bd0d536ce045a817d917be8"),
"name" : "Polo Grounds",
"location" : {
"type" : "Point",
"coordinates" : [
-73.9375,
40.8303
]
},
"category" : "Stadiums"
}

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:

db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
).pretty()

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:

2018-10-25T00:34:04.690+0300    done dumping mongospatial.collection (0 documents)
2018-10-25T00:34:04.690+0300 done dumping mongospatial.places (6 documents)

Step 7: Migrate the Geospatial Data to ApsaraDB for MongoDB

Access your already created instance using ssh command:

ssh root@<your_ip_address>

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

Next access ApsaraDB for MongoDB instance using:

mongo --host <domain>:<port> -u root -p <password> --authenticationDatabase admin

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

scp dump.rdb root@<your_ip_address>:/home

Finally, import the backup dump file using mongorestore command:

mongorestore

Step 8: Check if the data arrived

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

show dbsuse mongospatialdb.places.find().pretty();

You should see the following:

{
"_id" : ObjectId("5bd0d02c70dfbfa8d966579b"),
"name" : "Central Park",
"location" : {
"type" : "Point",
"coordinates" : [
-73.856077,
40.848447
]
}
}
{
"_id" : ObjectId("5bd0d40370dfbfa8d966579c"),
"name" : "Sara D. Roosevelt Park",
"location" : {
"type" : "Point",
"coordinates" : [
-73.9928,
40.7193
]
},
"category" : "Parks"
}
{
"_id" : ObjectId("5bd0d40370dfbfa8d966579d"),
"name" : "Polo Grounds",
"location" : {
"type" : "Point",
"coordinates" : [
-73.9375,
40.8303
]
},
"category" : "Stadiums"
}

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

Conclusion

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.

Reference:https://www.alibabacloud.com/blog/how-to-migrate-mongodb-geodatabase-to-apsaradb-for-mongodb_594590?spm=a2c41.12692354.0.0

Written by

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