Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0b18773
Create README.md
witold-swierzy May 29, 2025
3c5fb98
Update README.md
witold-swierzy May 29, 2025
ca73f48
Create README.md
witold-swierzy May 29, 2025
990abe0
Create README.md
witold-swierzy May 29, 2025
cdf9ac6
Create README.md
witold-swierzy May 29, 2025
f7712dc
Add files via upload
witold-swierzy May 29, 2025
da30def
Create README.md
witold-swierzy May 29, 2025
7b1808c
Create README.md
witold-swierzy May 29, 2025
4ae2124
Create README.md
witold-swierzy May 29, 2025
5f13720
Add files via upload
witold-swierzy May 29, 2025
52f00b2
Add files via upload
witold-swierzy May 29, 2025
c63524c
Add files via upload
witold-swierzy May 29, 2025
88de080
Create README.md
witold-swierzy May 29, 2025
ea2f391
Create README.md
witold-swierzy May 29, 2025
2bcbf5f
Add files via upload
witold-swierzy May 29, 2025
5959261
Create README.md
witold-swierzy May 29, 2025
4ddd009
Delete data-platform/autonomous-database/autonomous-json/oracle-api-f…
witold-swierzy May 29, 2025
afa8b59
Create README.md
witold-swierzy May 29, 2025
f2cd9d8
Create README.md
witold-swierzy May 29, 2025
55ca2f7
Add files via upload
witold-swierzy May 29, 2025
ec07ccd
Update README.md
witold-swierzy May 29, 2025
9eddee4
Add files via upload
witold-swierzy May 29, 2025
8f71ba4
Update README.md
witold-swierzy May 29, 2025
63d6297
Update README.md
witold-swierzy May 29, 2025
cefa378
Update README.md
witold-swierzy May 29, 2025
97d1b78
Update README.md
witold-swierzy May 29, 2025
cc435c7
Update README.md
witold-swierzy May 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## NodeJS demos
This directory contains NodeJS scripts using Oracle API for MongoDB and demonstrating its capabilities

## License

Copyright (c) 2025 Oracle and/or its affiliates.

Licensed under the Universal Permissive License (UPL), Version 1.0.

See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const {MongoClient} = require('mongodb');
const fs = require('fs');

async function displayExecutionPlan(db,collection,statement,hint) {
if (hint !== undefined)
result = await db.collection(collection).find(statement).hint(hint).explain();
else
result = await db.collection(collection).find(statement).explain();
console.log(result.queryPlanner);
}

async function displaySQLExecutionPlan(db,sqlStatement) {
result = db.aggregate([ {$sql: {statement: sqlStatement}}]).explain();
console.log((await result).stages[0].$sql);
}

async function getDBVersion(db) {
let oracle_api = !(await isNativeMongoDB(db));

if (oracle_api) {
result = db.aggregate([{ $sql: "select version_full from product_component_version" }] );
return (await result.toArray())[0].VERSION_FULL;
}
else
return (await db.admin().serverInfo()).version;
}

async function isNativeMongoDB(db) {
return !(await db.admin().serverInfo()).hasOwnProperty("oramlVersion");
}

async function prepareSchema(db) {
let data_set_dir = process.env.DATA_SET_DIR;
let oracle_api = !(await isNativeMongoDB(db));

// EMPLOYEES relational table cleaning up
if (oracle_api) {
result = db.aggregate([{ $sql: "truncate table employees"}]);
for await (res of result);
result = db.aggregate([{ $sql: "truncate table departments"}]);
for await (res of result);
}

// DEPARTMENTS_COL and EMPLOYEES_COL preparation
let departmentsArrayFile = data_set_dir + "/departments.json";
let employeesArrayFile = data_set_dir + "/employees.json";
let departments = JSON.parse(fs.readFileSync(departmentsArrayFile));
let employees = JSON.parse(fs.readFileSync(employeesArrayFile));

// dropping views
await db.dropCollection("DEPARTMENTS_COL_VW"); // view based on collection
if (oracle_api) {
await db.dropCollection("DEPARTMENTS_TAB_VW"); // JSON Collection view based on table
await db.dropCollection("DEPARTMENTS_DUAL_VW"); // JSON Duality View based on table
await db.dropCollection("EMPLOYEES_DUAL_VW"); // JSON Duality View based on table
await db.dropCollection("DEPT_EMP_COL");
}

// dropping collections
await db.dropCollection("DEPARTMENTS_COL");
await db.dropCollection("EMPLOYEES_COL");
await db.dropCollection("DEPARTMENTS_COL_BKP");

// load data into DEPARTMENTS_COL collection
await db.createCollection("DEPARTMENTS_COL");
await db.collection("DEPARTMENTS_COL").insertMany(departments);
// load data into EMPLOYEES_COL collection
await db.createCollection("EMPLOYEES_COL");
await db.collection("EMPLOYEES_COL").insertMany(employees);
}

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

module.exports = {displayExecutionPlan,displaySQLExecutionPlan,isNativeMongoDB,prepareSchema,getDBVersion,sleep};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const utils = require("./00_utils");
const {MongoClient} = require("mongodb");

async function clear_db() {
let client = new MongoClient(process.env.MONGO_URI);
let db = client.db();
let oracle_api = !(await utils.isNativeMongoDB(db));

let colls=[];
let collName = "";
let numColls = 0;

try {
console.log("Cleaning up the database schema.");
await utils.prepareSchema(db);
console.log("Database schema is clear.");
}
catch (e) {
console.error(e);
}
finally {
await client.close();
console.log("Disconnected from database.");
}
}

clear_db().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const utils = require("./00_utils");
const {MongoClient} = require("mongodb");

async function basic_demo() {
let client = new MongoClient(process.env.MONGO_URI);
let db = client.db();
let oracle_api = !(await utils.isNativeMongoDB(db));

let colls=[];
let collName = "";
let numColls = 0;

try {
console.log("Preparing the database schema.");
await utils.prepareSchema(db);
console.log("Database schema prepared.");

if (oracle_api)
console.log("You are connected to an Oracle MongoDB API service.");
else
console.log("You are connected to a native MongoDB database.");

// 1. list databases
databasesList = await db.admin().listDatabases();
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` ${db.name}`));

// 2. list collections in the default database
colls = (await db.listCollections().toArray());
console.log("Number of collections : "+colls.length);
console.log("Collections : ");
for ( i = 0; i < colls.length; i++ )
console.log(" "+colls[i].name);

// 3. insert documents into DEPARTMENTS_COL collection
console.log("Inserting department 120 - HR into DEPARTMENTS_COL collection");
db.collection("DEPARTMENTS_COL").insertOne(
{"_id" : 120, "department_name" : "Human Resources"}
);
console.log("Department 120, Human Resoures inserted.");

console.log("Inserting departments 130, 140 and 150 using insertMany method");
db.collection("DEPARTMENTS_COL").insertMany([
{"_id" : 130, "department_name" : "Department 130"},
{"_id" : 140, "department_name" : "Department 140"},
{"_id" : 150, "department_name" : "Department 150"}
]);
console.log("Departments 130, 140 and 150 inserted.");

// 4. reading data from DEPARTMENTS_COL collection
console.log("Reading all documents from DEPARTMENTS_COL collection");
depts = db.collection("DEPARTMENTS_COL").find().sort( {"_id" : 1} );
for await (dept of depts)
console.log("id : " + dept._id + " name : " + dept.department_name);

// 5. joining data from two collections
console.log("Query joining DEPARTMENTS_COL and EMPLOYEES_COL collections");
depts = db.collection("DEPARTMENTS_COL").aggregate
([{
$lookup : { from : "EMPLOYEES_COL",
localField : "_id",
foreignField : "department_id",
as : "EMPLOYEEES" }
}]);
for await (dept of depts) {
console.log(dept);
}

// 6. create a backup collection and copying the data
console.log("Creating DEPARTMENTS_COL_BKP collection.");
await db.createCollection("DEPARTMENTS_COL_BKP");
console.log("Collection DEPARTMENTS_COL_BKP created.");

console.log("Copying data from DEPARTMENTS_COL to DEPARTMENTS_COL_BKP.");
depts = db.collection("DEPARTMENTS_COL").find().sort( {"_id" : 1} );
for await (dept of depts) {
await db.collection("DEPARTMENTS_COL_BKP").insertOne(dept);
console.log("Department #"+dept._id+" copied to backup collection.")
}
console.log("Backup completed.");
console.log("Number of copied documents : " + (await db.collection("DEPARTMENTS_COL_BKP").countDocuments()));
}
catch (e) {
console.error(e);
}
finally {
await client.close();
console.log("Disconnected from database.");
}
}

basic_demo().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const utils = require("./00_utils");
const {MongoClient} = require("mongodb");

async function expr() {
let client = new MongoClient(process.env.MONGO_URI);
let db = client.db();
let oracle_api = !(await utils.isNativeMongoDB(db));

try {
console.log("Preparing the database schema.");
await utils.prepareSchema(db);
console.log("Database schema prepared.");

if (oracle_api)
console.log("You are connected to an Oracle MongoDB API service.");
else
console.log("You are connected to a native MongoDB database.");

if (!oracle_api) {
console.log("You are using native MongoDB service. $expr operator is fully supported.");
emps = db.collection("EMPLOYEES_COL").find({$expr: {$lt: ["$manager_id","$_id"]}});
}
else {
try {
console.log("Query using $expr operator.");
console.log("db.EMPLOYEES_COL.find({$expr: {$lt: ['$manager_id','$_id']}})")
emps = db.collection("EMPLOYEES_COL").find({$expr: {$lt: ["$manager_id","$_id"]}});
for await (emp of emps) {
console.log(emp.last_name + " " + emp.first_name);
}
}
catch (e) {
console.log("You are using Oracle MongoDB API. $epr operator has limited support.");
console.error(e);
}
console.log("You are using Oracle MongoDB API. There is need to use $sql operator instead of $expr.");
console.log("Query : select c.DATA from EMPLOYEES_COL c where c.DATA.manager_id < c.DATA.'_id'");
emps = db.aggregate([{ $sql: 'select c.DATA from EMPLOYEES_COL c where c.DATA.manager_id < c.DATA."_id"' }] );
console.log("Execution plan : ");
await utils.displaySQLExecutionPlan(db,'select c.DATA from EMPLOYEES_COL c where c.DATA.manager_id < c.DATA."_id"');
}
for await (emp of emps)
console.log(emp.last_name + " " + emp.first_name);
}
catch (e) {
console.error(e);
}
finally {
await client.close();
console.log("Disconnected from database.");
}
}

expr().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const utils = require("./00_utils");
const {MongoClient} = require("mongodb");
const fs = require('fs');

async function views() {
let client = new MongoClient(process.env.MONGO_URI);
let db = client.db();
let oracle_api = !(await utils.isNativeMongoDB(db));
let data_set_dir = process.env.DATA_SET_DIR;
let departmentsArrayFile = data_set_dir + "/departments.json";
let employeesArrayFile = data_set_dir + "/employees.json";

try {
console.log("Preparing the database schema.");
await utils.prepareSchema(db);
console.log("Database schema prepared.");

console.log("Creating view DEPARTMENTS_COL_VW based on DEPARTMENTS_COL collection");
await db.createCollection("DEPARTMENTS_COL_VW",{viewOn : "DEPARTMENTS_COL", pipeline : [] } );
console.log("DEPARTMENTS_COL_VW read-only view created.");


console.log("Checking the execution plan of a query using the view");
console.log("Query : db.DEPARTMENTS_COL_VW.find({{ '_id' : {$lte : 80 }}})");
depts = db.collection("DEPARTMENTS_COL_VW").find({ "_id" : {$lte : 80 }});
for await (dept of depts)
console.log(dept._id+" "+dept.department_name);
await utils.displayExecutionPlan(db,"DEPARTMENTS_COL_VW",{ "_id" : {$lte : 80 }});
try {
console.log("Trying to update DEPARTMENTS_COL_VW");
result = await db.collection("DEPARTMENTS_COL_VW").updateOne({_id:190},{$set:{department_name:"NoName"}});
console.log("Number of modified rows : "+result.modifiedCount);
if (oracle_api) {
console.log("You are using Oracle MongoDB API.");
console.log("Updates on collection views don't raise any exceptions, but are just ignored.");
}
}
catch (e) {
if (!oracle_api)
console.log("You are connected to a native MongoDB instance. Updates on VIEWS raise exceptions.");
console.error(e);
}
}
catch (e) {
console.error(e);
}
finally {
await client.close();
console.log("Disconnected from database.");
}
}

views().catch(console.error);
Loading