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 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:
If specifying latitude and longitude coordinates, list the longitude first and then latitude:
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }
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.
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
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
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 :
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 :
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"
}
MongoDB provides a number of geospatial query operators.
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()
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)
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
Initialize mongo shell on ECS instance and check if the database, collection and documents were successfully migrate:
show dbs
use mongospatial
db.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.
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.
Creating a Node.js Sequelize App PostgreSQL on Alibaba Cloud
Create a Serverless Website with Alibaba Cloud Function Compute
2,599 posts | 762 followers
FollowAlibaba Clouder - March 29, 2019
Alibaba Clouder - February 25, 2020
ApsaraDB - December 1, 2023
Alibaba Cloud MaxCompute - January 7, 2019
Alibaba Clouder - July 30, 2019
ApsaraDB - July 14, 2021
2,599 posts | 762 followers
FollowA secure, reliable, and elastically scalable cloud database service for automatic monitoring, backup, and recovery by time point
Learn MoreSupports data migration and data synchronization between data engines, such as relational database, NoSQL and OLAP
Learn MoreLearn More
More Posts by Alibaba Clouder