diff --git a/test/examples/aggregate.js b/test/examples/aggregate.js new file mode 100644 index 0000000000..e8413417c1 --- /dev/null +++ b/test/examples/aggregate.js @@ -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 + } + }); +}); diff --git a/test/examples/create_index.js b/test/examples/create_index.js new file mode 100644 index 0000000000..a5b4e5552a --- /dev/null +++ b/test/examples/create_index.js @@ -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 + } + }); +}); diff --git a/test/examples/run_command.js b/test/examples/run_command.js new file mode 100644 index 0000000000..38e7638896 --- /dev/null +++ b/test/examples/run_command.js @@ -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 + } + }); +});