-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (68 loc) · 2.13 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 }
}
];
const MDBURL = process.env.MONGODBURL;
MongoClient.connect(MDBURL)
.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);
changeStream.on("change", function(change) {
console.log(change);
});
setTimeout(function() {
collection.insert({ "batman": "bruce wayne" }, function(err) {
assert.ifError(err);
});
}, 1000);
setTimeout(function() {
collection.insert({ "superman": "clark kent" }, function(err) {
assert.ifError(err);
});
}, 2000);
setTimeout(function() {
collection.insert({ "wonder-woman": "diana prince" }, function(err) {
assert.ifError(err);
});
}, 3000);
setTimeout(function() {
collection.insert({ "ironman": "tony stark" }, function(err) {
assert.ifError(err);
});
}, 4000);
setTimeout(function() {
collection.insert({ "spiderman": "peter parker" }, function(err) {
assert.ifError(err);
});
}, 5000);
setTimeout(function() {
collection.updateOne(
{ "ironman": "tony stark" },
{ $set: { "ironman": "elon musk" } },
function(err) {
assert.ifError(err);
}
);
}, 6000);
setTimeout(function() {
collection.deleteOne({ "spiderman": "peter parker" }, function(err) {
assert.ifError(err);
});
}, 7000);
})
.catch(err => {
console.error(err);
});