Oracle Database API for MongoDB makes it possible to connect
to Oracle Autonomous Database using MongoDB
language drivers and tools.
Oracle Database API for MongoDB leverages the converged
database capabilities of an Autonomous Database to manage multiple data types, including JSON data, within a
single database. For example, these converged database capabilities allow you to use
SQL to query or update JSON data.
Note
MongoDB requires that you configure network access to use ACLs or that you define
a private endpoint for the Autonomous Database instance.
User Management for MongoDB Oracle Database API for MongoDB enables you to use an Oracle Autonomous Database as the data store. If you want or need to use an existing Autonomous Database for this purpose, here is the workflow.
Oracle Database API for MongoDB enables you to use an Oracle
Autonomous Database as the data
store.
To use the MongoDB API you can create and configure a new Autonomous Database or modify the
configuration of an existing Autonomous Database.
Note
MongoDB requires that you configure network access to use ACLs or that you define a
private endpoint for the Autonomous Database
instance. In addition to configuring the network access, you must enable MongoDB API on
the Autonomous Database instance.
Configure Access for MongoDB To use the MongoDB API, you can create and configure a new Autonomous Database or modify the configuration of an existing Autonomous Database by configuring ACLs or by defining a private endpoint.
To use the MongoDB API, you can create and configure a
new Autonomous Database or modify the
configuration of an existing Autonomous Database
by configuring ACLs or by defining a private endpoint.
Configure an Existing Autonomous Database for MongoDB
Open the Oracle Cloud
Infrastructure Console for your Autonomous Database
instance.
Note
To use Oracle Database API for MongoDB the Network must be configured and the
Access type must be either: Allow secure
access from specified IPs and VCNs or Virtual Cloud
Network.
After disabling any VPN, use the curl command: curl -s
https://ifconfig.me.
Note
Public IP addresses may change. Any change to your public IP address will require a change in the ACL. If you are unable to access your database, your ACL should be something you check.
ACLs Types and Use Cases
ACL Type
Use Case
Comment
IP Address
Local development laptops sharing the same public IP address
Easiest way to get started. Any laptop connected on this LAN will have access to the database with the database credentials.
CIDR Block
Local development laptop
Using IPv4/32 notation
IP Addresses separated by commas
Small number of local development laptops connected on distinct LANs (having distinct public IP addresses)
Can be tedious to manage with 10+ laptops.
CIDR Block
Local development laptops connected on the same subnet exposed to Internet (each laptop has its own public IP Address)
Rely on CIDR Block notation. See calculator here for more information. Example: 89.84.109.0/24 gives 256 possible IP addresses from 89.84.109.0 to 89.84.109.255
VCN with CIDR Block
For testing, production, or CI/CD pipeline hosted on OCI having their own VCN and Compute instances
Assign OCI compartment per environment type.
Mixing IP Address and VCN with CIDR Block
Local development laptop accessing a test Autonomous Database with connections from the testing environment or CI/CD pipeline
A common configuration option for on-going development work.
Oracle Database API for MongoDB
enables you to use an Oracle Autonomous Database as
the data store. If you want or need to use an existing Autonomous Database for this purpose, here is the workflow.
Oracle Database API for MongoDB enables the mapping of Autonomous Database objects to MongoDB objects as follows:
MongoDB Object
Oracle Autonomous Database Object
database
schema
collection
table
document
document (in a column)
For example, you could create a collection using the Oracle Database API for MongoDB as follows:
use scott;
db.createCollection('fruit');
A table named FRUIT is created in the schema SCOTT.
When you connect to the Oracle Database API for MongoDB, you authenticate using an Autonomous Database username and password. This authenticated connection then accesses collections within the corresponding schema. This user must meet the following requirements:
The user's schema must be ORDS-enabled, which is sometimes referred to as enabled for Web Access. See Basic Setup to Enable ORDS Database API for more information.
The role DWROLE in the Autonomous Database contains these roles, among others.
Access to schemas not granted to the user is prohibited. For example, the user SCOTT can only access collections in the schema SCOTT. There is one exception. If the authenticated user has the Autonomous Database privileges CREATE USER, ALTER USER and DROP USER, that user can access any ORDS-enabled schema.
Additionally, a user with these privileges can implicitly create schemas. That is, when the user creates a collection in a database that does not exist, the schema will be created automatically. See Oracle Database API for MongoDB for more information.
Create a Test Autonomous Database User for
MongoDB 🔗
The steps that follow use Database Actions to create a test user with the proper roles. You can also use Database Actions SQL or other SQL command line utilities to execute the SQL statements directly. See Create Users on Autonomous Database - Connecting with a Client Tool for more information.
Open the Oracle Cloud Infrastructure Console for your Autonomous Database.
Select Database Actions.
From the Database Actions Launchpad, select Administration > Database Users.
Select + Create User on the Database Users page.
Enter a User Name and Password
for your test user. Select Web Access, and set the
Quota on tablespace DATA.
Select the Granted Roles tab.
In addition to the default roles, select and add the SODA_APP role for the user.
$ mongosh --tls --tlsAllowInvalidCertificates 'mongodb://TESTUSER:<PASSWORD>@<database URL>.
<OCI region>.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&loadBalanced=false'
Current Mongosh Log ID: 614c9e2a01e3575c8c0b2ec7
Connecting to: mongodb://TESTUSER:<PASSWORD>@<database URL>.<OCIregion>.oraclecloudapps.com:27017/admin?
authMechanism=PLAIN&authSource=$external&tls=true&loadBalanced=false
Using MongoDB: 3.6.2
Using Mongosh: 1.0.7
For mongosh info see: https://docs.mongodb.com/mongodb-shell/admin
> show dbs
testuser 0 B
>
Note
Use URI percent-encoding to replace any reserved characters in your
connection-string URI — in particular, characters in your username and password. These are
the reserved characters and their percent encodings:
!
#
$
%
&
'
(
)
*
+
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
,
/
:
;
=
?
@
[
]
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
For example, if your username is RUTH and your password is
@least1/2#? then your MongoDB connection string to server
<server> might look like this:
Depending on the tools or drivers you use, you might be able to provide a username and
password as separate parameters, instead of as part of a URI connection string. In that case
you likely won't need to encode any reserved characters they contain.
$ mkdir autonomous_mongodb
$ cd autonomous_mongodb
$ npm init –y
Install mongodb dependency.
$ npm install mongodb
Create a JavaScript application named connect.js.
const { MongoClient } = require("mongodb");
const uri =
"mongodb://TESTUSER:<PASSWORD>@<Database URI>.<OCI region>.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&loadBalanced=false";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const database = client.db('admin');
const movies = database.collection('movies');
// Insert a movie
const doc = { title: 'Back to the Future',
year: 1985, genres: ['Adventure', 'Comedy', 'Sci-Fi'] }
const result = await movies.insertOne(doc);
// Query for a movie that has the title 'Back to the Future' :)
const query = { title: 'Back to the Future' };
const movie = await movies.findOne(query);
console.log(movie);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
Note
Use URI percent-encoding to replace any reserved characters in your
connection-string URI — in particular, characters in your username and password. These are
the reserved characters and their percent encodings:
!
#
$
%
&
'
(
)
*
+
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
,
/
:
;
=
?
@
[
]
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
For example, if your username is RUTH and your password is
@least1/2#? then your MongoDB connection string to server
<server> might look like this:
Depending on the tools or drivers you use, you might be able to provide a username and
password as separate parameters, instead of as part of a URI connection string. In that case
you likely won't need to encode any reserved characters they contain.