forked from shelfio/jest-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-aggregate.test.js
42 lines (35 loc) · 954 Bytes
/
mongo-aggregate.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const {MongoClient} = require('mongodb');
describe('insert', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
db = await connection.db();
});
afterAll(async () => {
await connection.close();
});
it('should aggregate docs from collection', async () => {
const files = db.collection('files');
await files.insertMany([
{type: 'Document'},
{type: 'Video'},
{type: 'Image'},
{type: 'Document'},
{type: 'Image'},
{type: 'Document'},
]);
const topFiles = await files
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}])
.toArray();
expect(topFiles).toEqual([
{_id: 'Document', count: 3},
{_id: 'Image', count: 2},
{_id: 'Video', count: 1},
]);
});
});