@@ -106,6 +106,41 @@ public void Causal_Consistency_Example_2()
106
106
// End Causal Consistency Example 2
107
107
}
108
108
}
109
+
110
+ [ Fact ]
111
+ public void Causal_Consistency_Example_3 ( )
112
+ {
113
+ RequireServer . Check ( ) . SupportsCausalConsistency ( ) ;
114
+
115
+ DropCollection ( CreateClient ( ) , "myDatabase" , "myCollection" ) ;
116
+
117
+ // Start Tunable Consistency Controls Example
118
+ var connectionString = "mongodb://localhost/?readPreference=secondaryPreferred" ;
119
+
120
+ var client = new MongoClient ( connectionString ) ;
121
+ var database = client . GetDatabase ( "myDatabase" ) ;
122
+ var collection = database . GetCollection < BsonDocument > ( "myCollection" ) ;
123
+
124
+ // Start client session, which is causally consistent by default
125
+ using ( var session = client . StartSession ( ) )
126
+ {
127
+ // Run causally related operations within the session
128
+ collection . InsertOne ( session , new BsonDocument ( "name" , "Tom" ) ) ;
129
+ collection . UpdateOne ( session ,
130
+ Builders < BsonDocument > . Filter . Eq ( "name" , "Tom" ) ,
131
+ Builders < BsonDocument > . Update . Set ( "name" , "John" ) ) ;
132
+
133
+ foreach ( var document in collection . Find ( session , FilterDefinition < BsonDocument > . Empty ) . ToEnumerable ( ) )
134
+ {
135
+ // process document
136
+ }
137
+ }
138
+ // End Tunable Consistency Controls Example
139
+
140
+ var result = collection . Find ( "{}" ) . FirstOrDefault ( ) ;
141
+ RemoveIds ( new [ ] { result } ) ;
142
+ result . Should ( ) . Be ( "{ name: 'John' }" ) ;
143
+ }
109
144
110
145
private IMongoClient CreateClient ( )
111
146
{
0 commit comments