Migrate MongoDB Geodatabase to ApsaraDB for MongoDB | Alibaba Cloud

In this tutorial, we will migrate a local MongoDB Geodatabase to ApsaraDB for MongoDB on Alibaba Cloud.

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

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. 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.
  2. 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.
  3. MongoDB, which is a NoSQL database that is open source and can be integrated with many open source tools such as QGIS.
  4. It delivers fast output for geospatial queries.

Prerequisites

Step 1: MongoDB Installation

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

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

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

  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

  • $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

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

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

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

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