From 436f0a91c08239fab3d04e339c085261bc51d824 Mon Sep 17 00:00:00 2001 From: Witold Swierzy Date: Thu, 23 Apr 2026 16:33:41 +0200 Subject: [PATCH 1/2] new functionalities --- .../NodeJS/hr/03_expr.js | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/03_expr.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/03_expr.js index f9c880736..c3972577f 100644 --- a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/03_expr.js +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/03_expr.js @@ -11,34 +11,15 @@ async function expr() { await utils.prepareSchema(db); console.log("Database schema prepared."); - if (oracle_api) + if (oracle_api) { console.log("You are connected to an Oracle MongoDB API service."); + console.log("This version of API supports $expr operator"); + } 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"'); - } + emps = db.collection("EMPLOYEES_COL").find({$expr: {$lt: ["$manager_id","$_id"]}}); + for await (emp of emps) console.log(emp.last_name + " " + emp.first_name); } From bb11551c855544b3aef77e9bede494b6abaa455d Mon Sep 17 00:00:00 2001 From: Witold Swierzy Date: Thu, 23 Apr 2026 16:37:45 +0200 Subject: [PATCH 2/2] new ORDS functionalities --- .../NodeJS/hr/06_partial_indexes.js | 56 +++++------------ .../NodeJS/hr/10_aggregations.js | 8 +-- .../NodeJS/hr/12_change_streams_consumer.js | 51 ++++++++++++++++ .../NodeJS/hr/12_change_streams_producer.js | 3 + .../NodeJS/hr/13_bsonSize.js | 35 +++++++++++ .../NodeJS/hr/14_lookup.js | 60 +++++++++++++++++++ 6 files changed, 167 insertions(+), 46 deletions(-) create mode 100644 data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_consumer.js create mode 100644 data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_producer.js create mode 100644 data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/13_bsonSize.js create mode 100644 data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/14_lookup.js diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/06_partial_indexes.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/06_partial_indexes.js index d0114c144..2b9b5633a 100644 --- a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/06_partial_indexes.js +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/06_partial_indexes.js @@ -16,51 +16,23 @@ async function partial_indexes() { console.log("Connection to an Oracle MongoDB API instance created."); else console.log("Connection to a native MongoDB instance created."); - - if (!oracle_api) { - console.log("You are connected to a native MongoDB. Partial indexes are natively supported."); - numOfIndexes = (await db.collection("EMPLOYEES_COL").aggregate([{$indexStats:{}}]).toArray()).length; - console.log("Number of indexes on EMPLOYEES_COL collections BEFORE INDEXING : "+numOfIndexes); + numOfIndexes = (await db.collection("EMPLOYEES_COL").aggregate([{$indexStats:{}}]).toArray()).length; + console.log("Number of indexes on EMPLOYEES_COL collections BEFORE INDEXING : "+numOfIndexes); - await db.collection("EMPLOYEES_COL").createIndex( { salary: 1, last_name: 1 }, + await db.collection("EMPLOYEES_COL").createIndex( { salary: 1, last_name: 1 }, { partialFilterExpression : { salary: { $lt: 8000 } } } ); - numOfIndexes = (await db.collection("EMPLOYEES_COL").aggregate([{$indexStats:{}}]).toArray()).length; - console.log("Index created succesfully."); - console.log("Number of indexes on EMPLOYEES_COL collections AFTER INDEXING : "+numOfIndexes); - - console.log("Execution plan, which uses partial index."); - console.log("Query: db.EMPLOYEES_COL.find({salary:4200})"); - emps = db.collection("EMPLOYEES_COL").find({salary:4200}); - console.log("Results : "); - for await (emp of emps) - console.log(emp._id+" "+emp.last_name+" "+emp.salary); - await utils.displayExecutionPlan(db,"EMPLOYEES_COL",{SALARY:42000},"salary_1_last_name_1"); - - } - else { - console.log("You are using Oracle MongoDB API. To create a partial index you need to use:"); - console.log("1. $sql operator"); - console.log("2. Function-based Indexes."); - - result = db.aggregate([{ $sql: "select * from user_indexes where index_type <> 'LOB' and table_name = 'EMPLOYEES_COL'" }] ) - numOfIndexes = (await result.toArray()).length; - console.log("Number of indexes on EMPLOYEES_COL collections BEFORE INDEXING : "+numOfIndexes); - - result = db.aggregate([ {$sql: {statement: "CREATE INDEX EMPLOYEES_SALARY_IDX on EMPLOYEES_COL(partial_value(DATA))"}}]); - for await (res of result); - - result = db.aggregate([{ $sql: "select * from user_indexes where index_type <> 'LOB' and table_name = 'EMPLOYEES_COL'" }]); - numOfIndexes = (await result.toArray()).length; - console.log("Number of indexes on EMPLOYEES_COL collections AFTER INDEXING : "+numOfIndexes); - console.log("Execution plan, which uses partial index."); - console.log("Query : select c.data from employees_col c where partial_value(data)=4200"); - emps = db.aggregate([{ $sql: "select c.data from employees_col c where partial_value(data)=4200" }] ); - console.log("Results : "); - for await (emp of emps) - console.log(emp._id+" "+emp.last_name+" "+emp.salary); - await utils.displaySQLExecutionPlan(db,"select c.data from employees_col c where partial_value(data)=4200"); - } + numOfIndexes = (await db.collection("EMPLOYEES_COL").aggregate([{$indexStats:{}}]).toArray()).length; + console.log("Index created succesfully."); + console.log("Number of indexes on EMPLOYEES_COL collections AFTER INDEXING : "+numOfIndexes); + + console.log("Execution plan, which uses partial index."); + console.log("Query: db.EMPLOYEES_COL.find({salary:4200})"); + emps = db.collection("EMPLOYEES_COL").find({salary:4200}); + console.log("Results : "); + for await (emp of emps) + console.log(emp._id+" "+emp.last_name+" "+emp.salary); + await utils.displayExecutionPlan(db,"EMPLOYEES_COL",{SALARY:42000},"salary_1_last_name_1"); } catch (e) { console.error(e); diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/10_aggregations.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/10_aggregations.js index 541fc4e75..2db2ade66 100644 --- a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/10_aggregations.js +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/10_aggregations.js @@ -30,10 +30,10 @@ async function aggregations() { result = await db.collection("EMPLOYEES_COL").aggregate([ { $group : { _id : '$department_id', avgsalary : { $avg : "$salary" } } } ]).explain(); - if (!oracle_api) - console.log(result.queryPlanner.winningPlan); - else - console.log(result.stages[0].$sql); + //if (!oracle_api) + // console.log(result.queryPlanner.winningPlan); + //else + // console.log(result.stages[0].$sql); } catch (e) { console.error(e); diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_consumer.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_consumer.js new file mode 100644 index 000000000..5189738c1 --- /dev/null +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_consumer.js @@ -0,0 +1,51 @@ +const utils = require("./00_utils"); +const {MongoClient} = require("mongodb"); + +async function change_streams_consumer() { + let client = new MongoClient(process.env.MONGO_URI); + let db = client.db(); + let session = client.startSession(); + let oracle_api = !(await utils.isNativeMongoDB(db)); + let db_version = await utils.getDBVersion(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. "); + else + console.log("You are connected to a native MongoDB instance."); + + console.log("Enabling changeStreams for DEPARTMENTS_COL collection"); + + var commandDoc = {collMod: "DEPARTMENTS_COL",preview:true, enableChangeStream:{preAndPost:true}}; + + var results = await db.command(commandDoc); + + console.log(results); + + console.log("changeStreams enabled"); + console.log("Starting to consume events produced by producer's code"); + + const collection = db.collection("DEPARTMENTS_COL"); + + const changeStream = collection.watch(); + // start listen to changes + console.log("Starting to consume events produced by producer's code"); + + for (var i=0; i < 25; i++) { + console.log(await changeStream.next()); + } + } + catch (e) { + console.error(e); + } + finally { + await client.close(); + console.log("Disconnected from database."); + } +} + +change_streams_consumer().catch(console.error); diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_producer.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_producer.js new file mode 100644 index 000000000..a97962ea8 --- /dev/null +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/12_change_streams_producer.js @@ -0,0 +1,3 @@ +// this code needs to be executed in mongosh + for (var i=200; i < 225; i++) + db.DEPARTMENTS_COL.insertOne({"_id" : i, "department_name" : "New Department #"+i}) diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/13_bsonSize.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/13_bsonSize.js new file mode 100644 index 000000000..6dda72f00 --- /dev/null +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/13_bsonSize.js @@ -0,0 +1,35 @@ +const utils = require("./00_utils"); +const {MongoClient} = require("mongodb"); + +async function bsonSize() { + let client = new MongoClient(process.env.MONGO_URI); + let db = client.db(); + let session = client.startSession(); + let oracle_api = !(await utils.isNativeMongoDB(db)); + let db_version = await utils.getDBVersion(db); + try { + console.log("Preparing the database schema."); + await utils.prepareSchema(db); + console.log("Database schema prepared."); + result = await db.collection("EMPLOYEES_COL").aggregate([ + { + "$project": { + "last_name": 1, + "object_size": { $bsonSize: "$$ROOT" } + } + } + ]); + console.log("Results : "); + for await (doc of result) + console.log(doc.last_name+" "+doc.object_size); + } + catch (e) { + console.error(e); + } + finally { + await client.close(); + console.log("Disconnected from database."); + } +} + +bsonSize().catch(console.error); \ No newline at end of file diff --git a/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/14_lookup.js b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/14_lookup.js new file mode 100644 index 000000000..f28b9cecc --- /dev/null +++ b/data-platform/autonomous-ai-database/autonomous-ai-json/oracle-api-for-mongodb-demos/NodeJS/hr/14_lookup.js @@ -0,0 +1,60 @@ +const utils = require("./00_utils"); +const {MongoClient} = require("mongodb"); + +async function lookup_example() { + let client = new MongoClient(process.env.MONGO_URI); + let db = client.db(); + let session = client.startSession(); + let oracle_api = !(await utils.isNativeMongoDB(db)); + let db_version = await utils.getDBVersion(db); + try { + console.log("Preparing the database schema."); + await utils.prepareSchema(db); + console.log("Database schema prepared."); + console.log("Simple lookup"); + result = await db.collection("EMPLOYEES_COL").aggregate( [ + { + $lookup: + { + from: "EMPLOYEES_COL", + localField: "_id", + foreignField: "department_id", + as: "EMPLOYEES" + } + } + ] ); + console.log("Results : "); + for await (doc of result) + console.log(doc); + console.log("Example with lookup, let and pipeline"); + console.log("This example requires database version 23.26.2"); + result = await db.collection("DEPARTMENTS_COL").aggregate( [ + { + $lookup: { + from: "EMPLOYEES_COL", + let: { deptno : "$_id"}, + pipeline: [ { + $match: { + $expr: { + $eq: [ "$$deptno", "$department_id" ] + } + } + } ], + as: "matches" + } + } + ] ); + console.log("Results : "); + for await (doc of result) + console.log(doc); + } + catch (e) { + console.error(e); + } + finally { + await client.close(); + console.log("Disconnected from database."); + } +} + +lookup_example().catch(console.error); \ No newline at end of file