-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeStreamDocument.cs
40 lines (36 loc) · 1.17 KB
/
ChangeStreamDocument.cs
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
var collection = new MongoClient("<connection-string>")
.GetDatabase("<database-name>")
.GetCollection<BsonDocument>("<collection-name>");
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>()
.Match(change =>
change.OperationType == ChangeStreamOperationType.Insert ||
change.OperationType == ChangeStreamOperationType.Update ||
change.OperationType == ChangeStreamOperationType.Replace
)
.AppendStage<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>, BsonDocument>(
@"{
$project: {
'_id': 1,
'fullDocument': 1,
'ns': 1,
'documentKey': 1
}
}"
);
ChangeStreamOptions options = new ()
{
FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
};
using IChangeStreamCursor<BsonDocument> enumerator = collection.Watch(
pipeline,
options
);
Console.WriteLine("Waiting for changes...");
while (enumerator.MoveNext())
{
IEnumerable<BsonDocument> changes = enumerator.Current;
foreach(BsonDocument change in changes)
{
Console.WriteLine(change);
}
}