Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

ORACLE-BASE - Oracle REST Data Services (ORDS) _ Oracle Database API for MongoDB

Uploaded by

Martine DOMPE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

ORACLE-BASE - Oracle REST Data Services (ORDS) _ Oracle Database API for MongoDB

Uploaded by

Martine DOMPE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23ai | Misc | PL/SQL |
SQL | RAC | WebLogic | Linux 
Home » Articles » Misc » Here

Oracle REST Data Services (ORDS) : Oracle Database API for


MongoDB
This article demonstrates the use of the Oracle database API for MongoDB functionality built into
Oracle REST Data Services (ORDS).

ORDS Setup
Database Setup
Create some JSON Collections (23ai optional)
MongoDB Compass
MongoDB Shell (mongosh)

Related articles.

Oracle REST Data Services (ORDS) : Standalone Mode (ORDS Version 22.1 Onward)
JSON Collections in Oracle Database 23ai
Oracle REST Data Services (ORDS) : Simple Oracle Document Access (SODA) for REST
Simple Oracle Document Access (SODA) for PL/SQL in Oracle Database 18c

ORDS Setup
We are going to assume you have an ORDS installation at version 22.3 or higher, running in
standalone mode. If not, you can find out how to install ORDS here.

Oracle REST Data Services (ORDS) : Standalone Mode (ORDS Version 22.1 Onward)

Enable the MongoDB API and start ORDS.

cd /u01/ords/bin
ords config set mongo.enabled true

ords --config ${ORDS_CONFIG} serve

During the startup we see a log message showing the MongoDB API connection URI.

2024-10-04T13:21:37.499Z INFO The Oracle API for MongoDB connection string is:
mongodb://[{user}:{password}@]localhost:27017/{user}?authMechanism=PLAIN&authSou

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 1/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Substituting our credentials we get the following. Remember to adjust the hostname if ORDS is
running on a different server.

mongodb://sodauser:sodauserpwd1@localhost:27017/sodauser?authMechanism=PLAIN&authSource=$

We are running with a self-signed certificate, so we also have to add


"&tlsAllowInvalidCertificates=true".

mongodb://sodauser:sodauserpwd1@localhost:27017/sodauser?authMechanism=PLAIN&authSource=$

ORDS is listening on port 27017 for the MongoDB API connections.

Database Setup
We create a test user to hold our MongoDB collections. It needs to be able to connect and create
tables for the basic functionality. We are going to build some additional objects, so we grant it
DB_DEVELOPER_ROLE. It's important to grant the SODA_APP role.

conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba

drop user if exists sodauser cascade;


create user sodauser identified by sodauserpwd1 quota unlimited on users;
grant db_developer_role to sodauser ;

grant soda_app to sodauser;

-- SELECT_CATALOG_ROLE isn't necessary.


grant select_catalog_role to testuser1;

We connect to the user and enable ORDS for the schema.

conn sodauser/sodauserpwd1@//localhost:1521/freepdb1

begin
ords.enable_schema(
p_enabled => TRUE,
p_schema => 'SODAUSER',
p_url_mapping_type => 'BASE_PATH',
p_url_mapping_pattern => 'sodauser',
p_auto_rest_auth => FALSE
);

commit;

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 2/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

end;
/

Create some JSON Collections (23ai optional)


We create some JSON collections. These are only available from 23ai onward. These are optional.

We create and populate some tables to work with.

drop table if exists emp purge;


drop table if exists dept purge;

create table dept (


deptno number(2) constraint pk_dept primary key,
dname varchar2(14),
loc varchar2(13)
) ;

create table emp (


empno number(4) constraint pk_emp primary key,
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2) constraint fk_deptno references dept
);

create index emp_dept_fk_i on emp(deptno);

insert into dept values (10,'ACCOUNTING','NEW YORK');


insert into dept values (20,'RESEARCH','DALLAS');
insert into dept values (30,'SALES','CHICAGO');
insert into dept values (40,'OPERATIONS','BOSTON');

insert into emp values (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,


insert into emp values (7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),16
insert into emp values (7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),125
insert into emp values (7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975
insert into emp values (7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1
insert into emp values (7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850
insert into emp values (7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450
insert into emp values (7788,'SCOTT','ANALYST',7566,to_date('13-JUL-87','dd-mm-rr')-85,30
insert into emp values (7839,'KING','PRESIDENT',null,to_date('17-11-1981','dd-mm-yyyy'),5
insert into emp values (7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),15
insert into emp values (7876,'ADAMS','CLERK',7788,to_date('13-JUL-87', 'dd-mm-rr')-51,110
insert into emp values (7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,n
insert into emp values (7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 3/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

insert into emp values (7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300


commit;

We create a JSON-relational duality view based on the EMP and DEPT tables.

create or replace json relational duality view departments_dv as


select json {'_id' : d.deptno,
'departmentName' : d.dname,
'location' : d.loc,
'employees' :
[ select json {'employeeNumber' : e.empno,
'employeeName' : e.ename,
'job' : e.job,
'salary' : e.sal}
from emp e with insert update delete
where d.deptno = e.deptno ]}
from dept d with insert update delete;

We create and populate a JSON collection table.

drop table if exists fruit_jct;

create json collection table fruit_jct;

insert into fruit_jct (data) values (json('{"fruit":"apple","quantity":10}'));


insert into fruit_jct (data) values (json('{"fruit":"oranges","quantity":5}'));
commit;

We create a JSON collection view based on the EMP and DEPT tables.

create or replace json collection view departments_jcv as


select json {'_id' : d.deptno,
'departmentName' : d.dname,
'location' : d.loc,
'employees' :
[ select json {'employeeNumber' : e.empno,
'employeeName' : e.ename,
'job' : e.job,
'salary' : e.sal}
from emp e
where d.deptno = e.deptno ]} as data
from dept d;

MongoDB Compass
Download MongoDB Compass from here.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 4/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Click the "Add new connection" button.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 5/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Put the amended URI into the dialog and click the "Save" button.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 6/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Expand the connection, which connects via the MongoDB API. Notice the JSON-relational duality
view and JSON collection table are visible as collections under the "sodauser" database.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 7/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

We click on the "DEPARTMENTS_DV" collection and we can expand the nodes to see the data.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 8/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

We switch the view to display the JSON.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 9/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

We can also see the data in a grid.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 10/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

We can also see the data in the JSON collection table.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 11/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Click the "+" button to insert a document.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 12/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

We can see the new document has been added.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 13/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

To add a new collection, hover over the "sodauser" tree node and click the "+" button that
appears. That opens the "Create Collection" screen.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 14/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

The new collection is now visible in the list. We could add some data to it, like we did previously.

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 15/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

If we check the database we can see a table has been created to hold the collection data.

select table_name
from user_tables
order by 1;

TABLE_NAME
--------------------------------------------------------------------------------
DEPT
EMP
FRUIT_JCT
my_collection

SQL>

In Oracle 23ai we can see it listed as a collection.

select collection_name, collection_type


from user_json_collections
order by 1;

COLLECTION_NAME COLLECTION_T
-------------------- ------------
https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 16/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

DEPARTMENTS_DV DUALITY VIEW


FRUIT_JCT TABLE
my_collection TABLE

SQL>

MongoDB Shell (mongosh)


Download MongoDB Shell (mongosh) from here.

Install mongosh by extracting it.

mkdir /u01/mongosh
cd /u01/mongosh
tar -xvzf /tmp/mongosh-2.3.1-linux-x64.tgz
cd /u01/mongosh/mongosh-2.3.1-linux-x64/bin

We start mongosh and connect to "sodauser" using the connection URI we discussed earlier.

$ ./mongosh 'mongodb://sodauser:sodauserpwd1@localhost:27017/sodauser?authMechanism=PLAIN
Current Mongosh Log ID: 67015d95c5d2ec5988964032
Connecting to: mongodb://@localhost:27017/sodauser?authMechanism=PLAIN&authSourc
Using MongoDB: 4.2.14
Using Mongosh: 2.3.1

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB perio
You can opt-out by running the disableTelemetry() command.

sodauser>

The following commands are all run from this command prompt.

We list the available collections using "show collections" or "show tables".

sodauser> show collections


DEPARTMENTS_DV
FRUIT_JCT
my_collection
sodauser>

sodauser> show tables


DEPARTMENTS_DV
FRUIT_JCT
https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 17/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

my_collection
sodauser>

We display the contents of the "FRUIT_JCT" collection.

sodauser> db.FRUIT_JCT.find();
[
{
_id: ObjectId('67002ac50000012114a94e3b'),
fruit: 'apple',
quantity: 10
},
{
_id: ObjectId('67002ac50000012114a94e3e'),
fruit: 'oranges',
quantity: 5
},
{
_id: ObjectId('6700344f74bd1bab031b545a'),
fruit: 'pineapple',
quantity: 20
}
]
sodauser>

We create a new collection called "my_collection2".

sodauser> db.createCollection('my_collection2');
{ ok: 1 }
sodauser>

We display the list of collections. Notice "my_collection2" has been added.

sodauser> show collections;


DEPARTMENTS_DV
FRUIT_JCT
my_collection
my_collection2
sodauser>

We insert data into the collection. Notice the collection name is references in the command.

sodauser> db.my_collection2.insertOne({"fruit":"apple","quantity":10});
{
acknowledged: true,
insertedId: ObjectId('670160d5c5d2ec5988964033')

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 18/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

}
sodauser>

sodauser> db.my_collection2.insertOne({"fruit":"banana","quantity":5});
{
acknowledged: true,
insertedId: ObjectId('67016100c5d2ec5988964034')
}
sodauser>

We display a specific document. First by searching for a specific element value, and second by
searching for a specific "_id" value.

sodauser> db.my_collection2.find({"fruit":"apple"});
[
{
_id: ObjectId('670160d5c5d2ec5988964033'),
fruit: 'apple',
quantity: 10
}
]
sodauser>

sodauser> db.my_collection2.find({"_id" : ObjectId('670160d5c5d2ec5988964033')});


[
{
_id: ObjectId('670160d5c5d2ec5988964033'),
fruit: 'apple',
quantity: 10
}
]
sodauser>

We update the document, setting a new quantity, then display the updated document.

sodauser> db.my_collection2.updateOne({"fruit":"apple"}, {$set: {quantity:20}});


{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
sodauser>

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 19/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

sodauser> db.my_collection2.find({"fruit":"apple"});
[
{
_id: ObjectId('670160d5c5d2ec5988964033'),
fruit: 'apple',
quantity: 20
}
]
sodauser>

We drop the collection called "my_collection2", and check it is removed from the collection list.

sodauser> db.my_collection2.drop();
true
sodauser>

sodauser> show collections;


DEPARTMENTS_DV
FRUIT_JCT
my_collection
sodauser>

We exit mongosh.

sodauser> exit
$

For more information see:

Oracle API for MongoDB Support


Oracle REST Data Services (ORDS) : Standalone Mode (ORDS Version 22.1 Onward)
JSON Collections in Oracle Database 23ai
Oracle REST Data Services (ORDS) : Simple Oracle Document Access (SODA) for REST
Simple Oracle Document Access (SODA) for PL/SQL in Oracle Database 18c

Hope this helps. Regards Tim...

Back to the Top.

Created: 2024-10-05 Updated: 2024-11-17

Contact Us


https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 20/21
02/12/2024 17:12 ORACLE-BASE - Oracle REST Data Services (ORDS) : Oracle Database API for MongoDB

Home | Articles | Scripts | Blog | Certification | Videos | Misc | About

About Tim Hall


Copyright & Disclaimer

https://oracle-base.com/articles/misc/oracle-rest-data-services-ords-oracle-database-api-for-mongodb 21/21

You might also like