Skip to content

Commit 6e896f4

Browse files
daprahamianmbroadst
authored andcommitted
docs: adding aggregation, createIndex, and runCommand examples
Fixes NODE-1323
1 parent cb3cd12 commit 6e896f4

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

test/examples/aggregate.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
'use strict';
2+
3+
const setupDatabase = require('../functional/shared').setupDatabase;
4+
const MongoClient = require('../../lib/mongo_client');
5+
6+
describe('examples.aggregaton:', function() {
7+
let client;
8+
let collection;
9+
10+
before(async function() {
11+
await setupDatabase(this.configuration);
12+
});
13+
14+
beforeEach(async function() {
15+
client = await MongoClient.connect(this.configuration.url());
16+
collection = client.db(this.configuration.db).collection('aggregateExample');
17+
});
18+
19+
afterEach(async function() {
20+
await client.close();
21+
client = undefined;
22+
collection = undefined;
23+
});
24+
25+
it('supports simple aggregation', {
26+
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
27+
test: async function() {
28+
// Start aggregate example 1
29+
await collection
30+
.aggregate([{ $match: { 'items.fruit': 'banana' } }, { $sort: { date: 1 } }])
31+
.toArray();
32+
// End aggregate example 1
33+
}
34+
});
35+
36+
it('supports $match, $group, $project, $unwind, $sum, $sort, $dayOfWeek', {
37+
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
38+
test: async function() {
39+
// Start aggregate example 2
40+
await collection
41+
.aggregate([
42+
{
43+
$unwind: '$items'
44+
},
45+
{
46+
$match: {
47+
'items.fruit': 'banana'
48+
}
49+
},
50+
{
51+
$group: {
52+
_id: { day: { $dayOfWeek: '$date' } },
53+
count: { $sum: '$items.quantity' }
54+
}
55+
},
56+
{
57+
$project: {
58+
dayOfWeek: '$_id.day',
59+
numberSold: '$count',
60+
_id: 0
61+
}
62+
},
63+
{
64+
$sort: { numberSold: 1 }
65+
}
66+
])
67+
.toArray();
68+
// End aggregate example 2
69+
}
70+
});
71+
72+
it('supports $unwind, $group, $sum, $dayOfWeek, $multiply, $project, $cond', {
73+
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
74+
test: async function() {
75+
// Start aggregate example 3
76+
await collection
77+
.aggregate([
78+
{
79+
$unwind: '$items'
80+
},
81+
{
82+
$group: {
83+
_id: { day: { $dayOfWeek: '$date' } },
84+
items_sold: { $sum: '$items.quantity' },
85+
revenue: { $sum: { $multiply: ['$items.quantity', '$items.price'] } }
86+
}
87+
},
88+
{
89+
$project: {
90+
day: '$_id.day',
91+
revenue: 1,
92+
items_sold: 1,
93+
discount: {
94+
$cond: { if: { $lte: ['$revenue', 250] }, then: 25, else: 0 }
95+
}
96+
}
97+
}
98+
])
99+
.toArray();
100+
// End aggregate example 3
101+
}
102+
});
103+
104+
it('supports $lookup, $filter, $match', {
105+
metadata: { requires: { mongodb: '>=2.8.0', topology: ['single'] } },
106+
test: async function() {
107+
// Start aggregate example 4
108+
await collection
109+
.aggregate([
110+
{
111+
$lookup: {
112+
from: 'air_airlines',
113+
let: { constituents: '$airlines' },
114+
pipeline: [
115+
{
116+
$match: { $expr: { $in: ['$name', '$$constituents'] } }
117+
}
118+
],
119+
as: 'airlines'
120+
}
121+
},
122+
{
123+
$project: {
124+
_id: 0,
125+
name: 1,
126+
airlines: {
127+
$filter: {
128+
input: '$airlines',
129+
as: 'airline',
130+
cond: { $eq: ['$$airline.country', 'Canada'] }
131+
}
132+
}
133+
}
134+
}
135+
])
136+
.toArray();
137+
// End aggregate example 4
138+
}
139+
});
140+
});

test/examples/create_index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const setupDatabase = require('../functional/shared').setupDatabase;
4+
const MongoClient = require('../../lib/mongo_client');
5+
6+
describe('examples.createIndex:', function() {
7+
let client;
8+
let collection;
9+
10+
before(async function() {
11+
await setupDatabase(this.configuration);
12+
});
13+
14+
beforeEach(async function() {
15+
client = await MongoClient.connect(this.configuration.url());
16+
collection = client.db(this.configuration.db).collection('createIndexExample');
17+
});
18+
19+
afterEach(async function() {
20+
await client.close();
21+
client = undefined;
22+
collection = undefined;
23+
});
24+
25+
it('supports building simple ascending index', {
26+
metadata: { requires: { topology: ['single'] } },
27+
test: async function() {
28+
// Start createIndex example 1
29+
await collection.createIndex({ score: 1 });
30+
// End createIndex example 1
31+
}
32+
});
33+
34+
it('supports building multikey index with partial filter expression', {
35+
metadata: { requires: { topology: ['single'], mongodb: '>=3.2.x' } },
36+
test: async function() {
37+
// Start createIndex example 2
38+
await collection.createIndex(
39+
{ cuisine: 1, name: 1 },
40+
{ partialFilterExpression: { rating: { $gt: 5 } } }
41+
);
42+
// End createIndex example 2
43+
}
44+
});
45+
});

test/examples/run_command.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const setupDatabase = require('../functional/shared').setupDatabase;
4+
const MongoClient = require('../../lib/mongo_client');
5+
6+
describe('examples.runCommand:', function() {
7+
let client;
8+
let db;
9+
10+
before(async function() {
11+
await setupDatabase(this.configuration);
12+
});
13+
14+
beforeEach(async function() {
15+
client = await MongoClient.connect(this.configuration.url());
16+
db = client.db(this.configuration.db);
17+
18+
// Done to ensure existence of collection
19+
await db.collection('restaurants').insertOne({});
20+
});
21+
22+
afterEach(async function() {
23+
await client.close();
24+
client = undefined;
25+
db = undefined;
26+
});
27+
28+
it('supports runCommand 1', {
29+
metadata: { requires: { topology: ['single'] } },
30+
test: async function() {
31+
// Start runCommand example 1
32+
await db.command({ buildInfo: 1 });
33+
// End runCommand example 1
34+
}
35+
});
36+
37+
it('supports runCommand 2', {
38+
metadata: { requires: { topology: ['single'] } },
39+
test: async function() {
40+
// Start runCommand example 2
41+
await db.command({ collStats: 'restaurants' });
42+
// End runCommand example 2
43+
}
44+
});
45+
});

0 commit comments

Comments
 (0)