-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (76 loc) · 2.12 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
import 'dotenv/config';
import { MongoClient } from 'mongodb';
const connectionString = `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@${process.env.MONGODB_HOST}:${process.env.MONGODB_PORT}`;
const client = new MongoClient(connectionString);
const dbName = 'hr';
async function main() {
await client.connect();
console.log('Connected successfully');
const db = client.db(dbName);
const employees = db.collection('employees');
const teams = db.collection('teams');
let result = await teams.insertMany([
{
name: "Product Management"
},
{
name: "Frontend Engineering"
}
]);
console.log(`inserted ids: ${result.insertedIds}`);
result = await employees.insertMany([
{
name: "Beege",
title: "Team Lead",
joined: "$date(2021-01-04)",
team: {
_id: result.insertedIds[0],
name: "Product Management"
}
},
{
name: "Andrei",
title: "Software Engineer",
joined: "$date(2021-06-01)",
team: {
_id: result.insertedIds[1],
name: "Frontend Engineering"
}
}
]);
console.log(`inserted ids: ${result.insertedIds}`);
let results = await employees.find(
{
"team.name": "Product Management"
},
{
projection: {name: 1, team: "$team.name", _id: 0}
}
).toArray();
console.log(`find results: ${results}`);
console.log(await results);
results = await employees.aggregate([
{
'$match': {
'name': {
'$exists': 1
}
}
}, {
'$group': {
'_id': '$team.name',
'countMembers': {
'$count': {}
}
}
}
]).toArray();
console.log(`pipeline results: ${results}`);
console.log(results);
return 'done';
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());