Quick Start Guide for MongoDB Migrations
Moving from MongoDB to Oracle is easier than you think. You can continue using your existing Mongo tools, and often your application runs by simply changing the connection string. This Quick Start Guide for MongoDB Migrations shows you how. You’ll learn to create an Oracle Autonomous Database, connect your Mongo tools, migrate your data, and use SQL to query your data. Let’s get started.
Table of Contents
- Create your Oracle Autonomous Database
- Retrieve the public access URL
- Connect with Mongo Compass
- Connect with Mongo Shell
- Migrate your data using Mongo Compass
- Create a view for data analysis
Note: If you already have an Autonomous Database setup for either JSON or transaction processing workloads, you can skip the Create your Oracle Autonomous Database step. Jump to retrieve your public access URL
Create your Oracle Autonomous Database
- Login to Oracle Cloud Infrastructure (OCI) account for your user and tenancy. If you do not have an OCI account, sign-up for a free OCI account. For more information, see the Oracle Always Free Resources page.
- Navigate to the Autonomous Database page and select the Create Autonomous Database button.
- In the Display name field, enter AJD1.
- In the Database name field, enter AJD1. The Database name must be unique for your tenancy.
- Accept the default value for Compartment.
- Select the Workload type as either JSON or Transaction Processing.
- Select the Choose database version as 23ai. Any supported version will work, but 23ai is preferred.
- Set the Password field. This password is used to connect via your Mongo tools.
- Re-enter the password into the Confirm password field.
- Select the Access type as Secure access for allowed IPs and VCNs only.
- Select the Add my IP address to IP value slider. The slider is disabled by default. When you enable it, it adds your current IP access to the Access control rule.
- Select the Create button.
Retrieve your public access URL
- Navigate to the Autonomous Database page.
- Select the link on the Display name for your Autonomous Database.
- Select Tool configuration from the menu tabs.
- Navigate (scroll) in the database details to the section on MongoDB API. Select the Copy link for the Public access URL. Save this copied URL to a text editor.
Connect with Mongo Compass
- Open Mongo Compass and select the Add new connection button.
- Copy the public access URL that you retrieved and saved in the previous section into the URI field in Mongo Compass’ New Connection window. Edit the copied URL, replace the [user:password@] with ADMIN:<your database’s ADMIN password>@ in the connection string.
- In the Name field, enter AJD1.
- Select the Save & Connect button.
- Your connection will be saved and a connection will be established with your Oracle Database.
- You can create a database and import your collection(s). For test JSON data sets, see the Oracle Github repository.
Connect with Mongo Shell
-
From a terminal, command line, or shell, run mongosh with the public access URL. Replace the [user:password@] with ADMIN:<your database’s ADMIN password>@ in the connection string. Replace [user] as needed for your Oracle Database.
mongosh 'mongodb://<your-oracle-OCID-and-DB-URL>.oraclecloudapps.com:27017/[user]? authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'
-
If you do not want to include the password, you can use the following command from a terminal, command line, or shell, run mongosh with the public access URL. Replace [user] as needed for your Oracle Database.
mongosh -u ADMIN 'mongodb://<your-oracle-OCID-and-DB-URL>.oraclecloudapps.com:27017/[user]? authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'
Migrate your data using Mongo Compass
- Open Mongo Compass and connect to the database to export. If you want to use the data set used in these steps, see Sample AirBnB Listings Dataset for more information.
- Open the collection you want to export, and select the EXPORT DATA button and the Export the full collection menu item.
- In the Export dialog window, select the file name and location for your .json file and then select the the Export… button.
- The export processes, and it shows you how many documents were written into your .json file.
- Using Mongo Compass, connect to your Oracle Database. Select the + (Create database) icon.
- In the Create Database dialog window, enter a Database Name and a Collection Name and then select the Create Database button.
- From the newly creating collection, select the Import Data button.
- Select the file in the file location where you saved the database export .json file, and then select the Import button.
- Once the import completes, it shows you how many documents were imported into your new collection.
- Login to Oracle Cloud Infrastructure (OCI) account for your user and tenancy. Navigate to the Autonomous Database page and select the link in Display Name for your Oracle Database.
- Select the Tool configuration tab and the Database actions Copy button.
- In your browser, paste the Database actions public access URL. Select the SQL tool and then select the Open button.
- In the SQL Navigator, select the schema.
-
Run this query for the collection. Your count should match your expected value.
SELECT COUNT(*) FROM MIGRATED_AIRBNB.LISTINGSANDREVIEWS;
Create a view for data analysis
With your data stored in your Oracle Autonomous Database, you can create SQL views on top of your JSON collections which enables relational-style SQL queries on complex SQL-JSON views and hierarchies. Beyond this simple example, using the power of Oracle Database 23ai you can make your JSON data “AI ready” for use with new Oracle AI features such as AI Vector Search, RAG, JSON Relational Duality, and Select AI.
- Login to your database and go to the Database actions SQL tool.
- Select the schema (
MIGRATED_AIRBNB
) for your data.
-
Create a SQL view (AIRBNB_LISTINGS_VIEW) using JSON_VALUE to define columns.
-- create the Oracle SQL view on top of the JSON collection CREATE OR REPLACE VIEW MIGRATED_AIRBNB.AIRBNB_LISTINGS_VIEW AS SELECT json_value(data, '$._id' RETURNING NUMBER) AS listing_id, json_value(data, '$.name' RETURNING VARCHAR2(200)) AS name, json_value(data, '$.price' RETURNING NUMBER) AS price, json_value(data, '$.address.government_area' RETURNING VARCHAR2(100)) AS city, json_value(data, '$.room_type' RETURNING VARCHAR2(50)) AS room_type, json_value(data, '$.availability.availability_365' RETURNING NUMBER) AS available_days FROM MIGRATED_AIRBNB.LISTINGSANDREVIEWS;
-
Test your SQL view.
--query the SQL view select * from migrated_airbnb.airbnb_listings_view;
-
Calculate the # of listings, average listing price, and total price of all listings by city.
select v.city as "City", count(*) as "# of Listings", round(avg(v.price),1) as "Average Listing Price", sum(v.price) as "Total City Listings" from migrated_airbnb.airbnb_listings_view v group by v.city order by 2 desc, 3 desc, 1