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