-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
77 lines (67 loc) · 2.35 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
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
/*
Modify Change Stream Output using Aggregation Pipelines
You can control change stream output by providing an array of one or more of the following pipeline stages when configuring the change stream:
$match, $project, $addFields, $replaceRoot, $redact
See Change Events for more information on the change stream response document format.
https://docs.mongodb.com/manual/reference/change-events/#change-stream-output
*/
const pipeline = [
{
$project: { documentKey: false }
}
];
MongoClient.connect("mongodb://localhost:27017,localhost:27018,localhost:27019?replicaSet=mongo-repl")
.then(client => {
console.log("Connected correctly to server");
// specify db and collections
const db = client.db("superheroesdb");
const collection = db.collection("superheroes");
const changeStream = collection.watch(pipeline);
// start listen to changes
changeStream.on("change", function (change) {
console.log(change);
});
// insert few data with timeout so that we can watch it happening
setTimeout(function () {
collection.insertOne({ "batman": "bruce wayne" }, function (err) {
assert.ifError(err);
});
}, 1000);
setTimeout(function () {
collection.insertOne({ "superman": "clark kent" }, function (err) {
assert.ifError(err);
});
}, 2000);
setTimeout(function () {
collection.insertOne({ "wonder-woman": "diana prince" }, function (err) {
assert.ifError(err);
});
}, 3000);
setTimeout(function () {
collection.insertOne({ "ironman": "tony stark" }, function (err) {
assert.ifError(err);
});
}, 4000);
setTimeout(function () {
collection.insertOne({ "spiderman": "peter parker" }, function (err) {
assert.ifError(err);
});
}, 5000);
// update existing document
setTimeout(function () {
collection.updateOne({ "ironman": "tony stark" }, { $set: { "ironman": "elon musk" } }, function (err) {
assert.ifError(err);
});
}, 6000);
// delete existing document
setTimeout(function () {
collection.deleteOne({ "spiderman": "peter parker" }, function (err) {
assert.ifError(err);
});
}, 7000);
})
.catch(err => {
console.error(err);
});