How to Migrate MongoDB Geodatabase to ApsaraDB for MongoDB

MongoDB Geospatial Support

  • 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

Step 1: MongoDB Installation

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
$ 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
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
$ sudo systemctl start mongod
$ sudo systemctl status mongod
● 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)
$ sudo systemctl enable mongod

Step 2: Create a New Database

$ mongo
use mongospatial
show dbs

Step 3: Populate MongoDB Database

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"
} );
  • 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
db.collection.createIndex({location:"2dsphere")
db.places.find().pretty()
{
"_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.
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
).pretty()

Step 6: Creating a Backup File

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>
mongo --host <domain>:<port> -u root -p <password> --authenticationDatabase admin
scp dump.rdb root@<your_ip_address>:/home
mongorestore

Step 8: Check if the data arrived

show dbsuse mongospatialdb.places.find().pretty();
{
"_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"
}

Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introduction to No Sql Database

Integrate and trigger a Jenkins job from Slack

#SideNotes — Stack — Abstract Data Type and Data Structure

Wordpress inside a Ruby on Rails app

Creating my personal website with Astro, Tailwind CSS, and Nx

Programming integrations

The Hashchess Project

Test Driven

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
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

How can I manage my Kafka Artefacts?

How to setup and launch Kafka Connect/Connector

Securing Sensitive Details of Your Containerized Docker Applications

CSV file in S3 from a Trino Query