Skip to content

Commit c63524c

Browse files
Add files via upload
1 parent 52f00b2 commit c63524c

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const {MongoClient} = require('mongodb');
2+
const fs = require('fs');
3+
4+
async function displayExecutionPlan(db,collection,statement,hint) {
5+
if (hint !== undefined)
6+
result = await db.collection(collection).find(statement).hint(hint).explain();
7+
else
8+
result = await db.collection(collection).find(statement).explain();
9+
console.log(result.queryPlanner);
10+
}
11+
12+
async function displaySQLExecutionPlan(db,sqlStatement) {
13+
result = db.aggregate([ {$sql: {statement: sqlStatement}}]).explain();
14+
console.log((await result).stages[0].$sql);
15+
}
16+
17+
async function getDBVersion(db) {
18+
let oracle_api = !(await isNativeMongoDB(db));
19+
20+
if (oracle_api) {
21+
result = db.aggregate([{ $sql: "select version_full from product_component_version" }] );
22+
return (await result.toArray())[0].VERSION_FULL;
23+
}
24+
else
25+
return (await db.admin().serverInfo()).version;
26+
}
27+
28+
async function isNativeMongoDB(db) {
29+
return !(await db.admin().serverInfo()).hasOwnProperty("oramlVersion");
30+
}
31+
32+
async function prepareSchema(db) {
33+
let data_set_dir = process.env.DATA_SET_DIR;
34+
35+
// refreshing PURCHASEORDER_COL collection
36+
result = db.aggregate([{ $sql: "drop table PURCHASEORDERS_COL cascade constraints"}]);
37+
for await (res of result);
38+
39+
result = db.aggregate([{ $sql: "create json collection table PURCHASEORDERS_COL"}]);
40+
for await (res of result);
41+
42+
result = db.aggregate([{ $sql: "begin "+
43+
" DBMS_CLOUD.copy_collection(collection_name => 'PURCHASEORDERS_COL',"+
44+
" credential_name => 'AJD_CRED',"+
45+
" file_uri_list => '<URL for PURCHASEORDERS_COL.json file>',"+
46+
" format => json_object('recorddelimiter' value '''\n'''));"+
47+
"end;" }] );
48+
for await (res of result);
49+
}
50+
51+
function sleep(ms) {
52+
return new Promise((resolve) => setTimeout(resolve, ms));
53+
}
54+
55+
module.exports = {displayExecutionPlan,displaySQLExecutionPlan,isNativeMongoDB,prepareSchema,getDBVersion,sleep};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { exit } = require("process");
2+
const utils = require("./00_utils");
3+
const {MongoClient} = require("mongodb");
4+
const fs = require('fs');
5+
6+
async function load_json_file() {
7+
let client = new MongoClient(process.env.MONGO_URI);
8+
let db = client.db();
9+
let oracle_api = !(await utils.isNativeMongoDB(db));
10+
11+
if (!oracle_api) {
12+
console.log("This demo can be executed against Oracle API for MongoDB only!");
13+
await client.close();
14+
exit(0);
15+
}
16+
17+
try {
18+
console.log("You are connected to an instance of Oracle API for MongoDB. Starting data loading process.");
19+
result = db.aggregate([{ $sql: "drop table if exists PURCHASEORDERS_COL cascade constraints"}]);
20+
for await (res of result);
21+
22+
result = db.aggregate([{ $sql: "create json collection table PURCHASEORDERS_COL"}]);
23+
numOfDocs = await db.collection("PURCHASEORDERS_COL").countDocuments();
24+
console.log("Number of documents before loading : "+numOfDocs);
25+
console.log("Starting loading data.");
26+
result = db.aggregate([{ $sql: "begin "+
27+
" DBMS_CLOUD.copy_collection(collection_name => 'PURCHASEORDERS_COL',"+
28+
" credential_name => 'AJD_CRED',"+
29+
" file_uri_list => '<URL for PURCHASEORDERS_COL.json file>',"+
30+
" format => json_object('recorddelimiter' value '''\n'''));"+
31+
"end;" }] );
32+
for await (res of result);
33+
numOfDocs = await db.collection("PURCHASEORDERS_COL").countDocuments();
34+
console.log("Data loaded.");
35+
console.log("Number of loaded documents : "+numOfDocs);
36+
}
37+
catch (e) {
38+
console.error(e);
39+
}
40+
finally {
41+
await client.close();
42+
console.log("Disconnected from database.");
43+
}
44+
}
45+
46+
load_json_file().catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { exit } = require("process");
2+
const utils = require("./00_utils");
3+
const {MongoClient} = require("mongodb");
4+
const fs = require('fs');
5+
6+
async function partition_test() {
7+
let client = new MongoClient(process.env.MONGO_URI);
8+
let db = client.db();
9+
let oracle_api = !(await utils.isNativeMongoDB(db));
10+
11+
if (!oracle_api) {
12+
console.log("This demo can be executed against Oracle API for MongoDB only!");
13+
await client.close();
14+
exit(0);
15+
}
16+
17+
try {
18+
console.log("You are connected to an instance of Oracle API for MongoDB. Starting the demonstration.");
19+
console.log("Preparing the schema");
20+
await utils.prepareSchema(db);
21+
console.log("Schema prepared");
22+
23+
result = db.aggregate([{ $sql: "DROP TABLE IF EXISTS ORDERS_COL"}]);
24+
for await (res of result);
25+
console.log("ORDERS_COL dropped.");
26+
27+
result = db.aggregate([{ $sql: "CREATE JSON COLLECTION TABLE ORDERS_COL"}]);
28+
for await (res of result);
29+
console.log("ORDERS_COL created.");
30+
31+
result = db.aggregate([{$sql: "INSERT INTO ORDERS_COL SELECT * FROM PURCHASEORDERS_COL"}]);
32+
for await (res of result);
33+
console.log("Data into ORDERS_COL loaded.");
34+
35+
result = db.aggregate([{ $sql: "ALTER TABLE ORDERS_COL "+
36+
"ADD (po_num_vc NUMBER GENERATED ALWAYS AS "+
37+
" (json_value (DATA, '$.PONumber.number()' "+
38+
" ERROR ON ERROR)))" }]);
39+
for await (res of result);
40+
console.log("Partition key added.");
41+
42+
result = db.aggregate([{$sql: "ALTER TABLE ORDERS_COL "+
43+
"MODIFY PARTITION BY RANGE (po_num_vc) "+
44+
"(PARTITION p1 VALUES LESS THAN (1000), "+
45+
"PARTITION p2 VALUES LESS THAN (2000), "+
46+
"PARTITION p3 VALUES LESS THAN (3000), "+
47+
"PARTITION p4 VALUES LESS THAN (4000), "+
48+
"PARTITION p5 VALUES LESS THAN (5000), "+
49+
"PARTITION p6 VALUES LESS THAN (6000), "+
50+
"PARTITION p7 VALUES LESS THAN (7000), "+
51+
"PARTITION p8 VALUES LESS THAN (8000), "+
52+
"PARTITION p9 VALUES LESS THAN (9000), "+
53+
"PARTITION p10 VALUES LESS THAN (10000), "+
54+
"PARTITION p11 VALUES LESS THAN (11000))"
55+
}]);
56+
for await (res of result);
57+
console.log("Table ORDERS_COL partitioned.");
58+
59+
await utils.displayExecutionPlan(db,"ORDERS_COL",{ "PO_NUM_VC" : {$lte : 800 }});
60+
61+
}
62+
catch (e) {
63+
console.error(e);
64+
}
65+
finally {
66+
await client.close();
67+
console.log("Disconnected from database.");
68+
}
69+
}
70+
71+
partition_test().catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
Error starting at line : 1 in command -
3+
select.
4+
Error report -
5+
Unknown Command
6+

data-platform/autonomous-database/autonomous-json/oracle-api-for-mongodb-demos/NodeJS/po/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"mongoose-execution-time": "^1.1.1"
4+
}
5+
}

0 commit comments

Comments
 (0)