Skip to content

Commit

Permalink
docs: adding aggregation, createIndex, and runCommand examples
Browse files Browse the repository at this point in the history
Fixes NODE-1323
  • Loading branch information
daprahamian authored and mbroadst committed Jan 19, 2019
1 parent cb3cd12 commit 6e896f4
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
140 changes: 140 additions & 0 deletions test/examples/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
'use strict';

const setupDatabase = require('../functional/shared').setupDatabase;
const MongoClient = require('../../lib/mongo_client');

describe('examples.aggregaton:', function() {
let client;
let collection;

before(async function() {
await setupDatabase(this.configuration);
});

beforeEach(async function() {
client = await MongoClient.connect(this.configuration.url());
collection = client.db(this.configuration.db).collection('aggregateExample');
});

afterEach(async function() {
await client.close();
client = undefined;
collection = undefined;
});

it('supports simple aggregation', {
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
test: async function() {
// Start aggregate example 1
await collection
.aggregate([{ $match: { 'items.fruit': 'banana' } }, { $sort: { date: 1 } }])
.toArray();
// End aggregate example 1
}
});

it('supports $match, $group, $project, $unwind, $sum, $sort, $dayOfWeek', {
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
test: async function() {
// Start aggregate example 2
await collection
.aggregate([
{
$unwind: '$items'
},
{
$match: {
'items.fruit': 'banana'
}
},
{
$group: {
_id: { day: { $dayOfWeek: '$date' } },
count: { $sum: '$items.quantity' }
}
},
{
$project: {
dayOfWeek: '$_id.day',
numberSold: '$count',
_id: 0
}
},
{
$sort: { numberSold: 1 }
}
])
.toArray();
// End aggregate example 2
}
});

it('supports $unwind, $group, $sum, $dayOfWeek, $multiply, $project, $cond', {
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
test: async function() {
// Start aggregate example 3
await collection
.aggregate([
{
$unwind: '$items'
},
{
$group: {
_id: { day: { $dayOfWeek: '$date' } },
items_sold: { $sum: '$items.quantity' },
revenue: { $sum: { $multiply: ['$items.quantity', '$items.price'] } }
}
},
{
$project: {
day: '$_id.day',
revenue: 1,
items_sold: 1,
discount: {
$cond: { if: { $lte: ['$revenue', 250] }, then: 25, else: 0 }
}
}
}
])
.toArray();
// End aggregate example 3
}
});

it('supports $lookup, $filter, $match', {
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
test: async function() {
// Start aggregate example 4
await collection
.aggregate([
{
$lookup: {
from: 'air_airlines',
let: { constituents: '$airlines' },
pipeline: [
{
$match: { $expr: { $in: ['$name', '$$constituents'] } }
}
],
as: 'airlines'
}
},
{
$project: {
_id: 0,
name: 1,
airlines: {
$filter: {
input: '$airlines',
as: 'airline',
cond: { $eq: ['$$airline.country', 'Canada'] }
}
}
}
}
])
.toArray();
// End aggregate example 4
}
});
});
45 changes: 45 additions & 0 deletions test/examples/create_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const setupDatabase = require('../functional/shared').setupDatabase;
const MongoClient = require('../../lib/mongo_client');

describe('examples.createIndex:', function() {
let client;
let collection;

before(async function() {
await setupDatabase(this.configuration);
});

beforeEach(async function() {
client = await MongoClient.connect(this.configuration.url());
collection = client.db(this.configuration.db).collection('createIndexExample');
});

afterEach(async function() {
await client.close();
client = undefined;
collection = undefined;
});

it('supports building simple ascending index', {
metadata: { requires: { topology: ['single'] } },
test: async function() {
// Start createIndex example 1
await collection.createIndex({ score: 1 });
// End createIndex example 1
}
});

it('supports building multikey index with partial filter expression', {
metadata: { requires: { topology: ['single'], mongodb: '>=3.2.x' } },
test: async function() {
// Start createIndex example 2
await collection.createIndex(
{ cuisine: 1, name: 1 },
{ partialFilterExpression: { rating: { $gt: 5 } } }
);
// End createIndex example 2
}
});
});
45 changes: 45 additions & 0 deletions test/examples/run_command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const setupDatabase = require('../functional/shared').setupDatabase;
const MongoClient = require('../../lib/mongo_client');

describe('examples.runCommand:', function() {
let client;
let db;

before(async function() {
await setupDatabase(this.configuration);
});

beforeEach(async function() {
client = await MongoClient.connect(this.configuration.url());
db = client.db(this.configuration.db);

// Done to ensure existence of collection
await db.collection('restaurants').insertOne({});
});

afterEach(async function() {
await client.close();
client = undefined;
db = undefined;
});

it('supports runCommand 1', {
metadata: { requires: { topology: ['single'] } },
test: async function() {
// Start runCommand example 1
await db.command({ buildInfo: 1 });
// End runCommand example 1
}
});

it('supports runCommand 2', {
metadata: { requires: { topology: ['single'] } },
test: async function() {
// Start runCommand example 2
await db.command({ collStats: 'restaurants' });
// End runCommand example 2
}
});
});

0 comments on commit 6e896f4

Please sign in to comment.