Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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})
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);