-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: adding aggregation, createIndex, and runCommand examples
Fixes NODE-1323
- Loading branch information
1 parent
cb3cd12
commit 6e896f4
Showing
3 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
}); |