1 | const {MongoClient} = require('mongodb');
|
2 |
|
3 | describe('insert', () => {
|
4 | let connection;
|
5 | let db;
|
6 |
|
7 | beforeAll(async () => {
|
8 | connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true});
|
9 | db = await connection.db(global.__MONGO_DB_NAME__);
|
10 | });
|
11 |
|
12 | afterAll(async () => {
|
13 | await connection.close();
|
14 | await db.close();
|
15 | });
|
16 |
|
17 | it('should aggregate docs from collection', async () => {
|
18 | const files = db.collection('files');
|
19 |
|
20 | await files.insertMany([
|
21 | {type: 'Document'},
|
22 | {type: 'Video'},
|
23 | {type: 'Image'},
|
24 | {type: 'Document'},
|
25 | {type: 'Image'},
|
26 | {type: 'Document'}
|
27 | ]);
|
28 |
|
29 | const topFiles = await files
|
30 | .aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}])
|
31 | .toArray();
|
32 |
|
33 | expect(topFiles).toEqual([
|
34 | {_id: 'Document', count: 3},
|
35 | {_id: 'Image', count: 2},
|
36 | {_id: 'Video', count: 1}
|
37 | ]);
|
38 | });
|
39 | });
|