1313* limitations under the License.
1414*/
1515
16+ using System ;
17+ using MongoDB . Bson ;
18+ using Xunit ;
19+
1620namespace MongoDB . Driver . Examples
1721{
1822 public class VersionedApiExamples
1923 {
24+ [ Fact ]
2025 public void ConfigureServerApi ( )
2126 {
2227 // Start Versioned API Example 1
@@ -28,6 +33,7 @@ public void ConfigureServerApi()
2833 // End Versioned API Example 1
2934 }
3035
36+ [ Fact ]
3137 public void ConfigureServerApiStrict ( )
3238 {
3339 // Start Versioned API Example 2
@@ -39,6 +45,7 @@ public void ConfigureServerApiStrict()
3945 // End Versioned API Example 2
4046 }
4147
48+ [ Fact ]
4249 public void ConfigureServerApiNonStrict ( )
4350 {
4451 // Start Versioned API Example 3
@@ -50,6 +57,7 @@ public void ConfigureServerApiNonStrict()
5057 // End Versioned API Example 3
5158 }
5259
60+ [ Fact ]
5361 public void ConfigureServerApiDeprecationErrors ( )
5462 {
5563 // Start Versioned API Example 4
@@ -60,5 +68,52 @@ public void ConfigureServerApiDeprecationErrors()
6068 var mongoClient = new MongoClient ( mongoClientSettings ) ;
6169 // End Versioned API Example 4
6270 }
71+
72+ [ Fact ]
73+ public void VersionedAPI_Strict_Migration_Example ( )
74+ {
75+ var connectionString = "mongodb://localhost" ;
76+ var serverApi = new ServerApi ( ServerApiVersion . V1 , strict : true ) ;
77+ var mongoClientSettings = MongoClientSettings . FromConnectionString ( connectionString ) ;
78+ mongoClientSettings . ServerApi = serverApi ;
79+ var mongoClient = new MongoClient ( mongoClientSettings ) ;
80+ var database = mongoClient . GetDatabase ( "test" ) ;
81+ database . DropCollection ( "coll" ) ;
82+ var collection = database . GetCollection < BsonDocument > ( "coll" ) ;
83+
84+ // 1. Populate a test sales collection:
85+ collection . InsertMany (
86+ new [ ]
87+ {
88+ BsonDocument . Parse ( @"{ ""_id"" : 1, ""item"" : ""ab"", ""price"" : 10, ""quantity"" : 2, ""date"" : ISODate(""2021-01-01T08:00:00Z"") }" ) ,
89+ BsonDocument . Parse ( @"{ ""_id"" : 2, ""item"" : ""jkl"", ""price"" : 20, ""quantity"" : 1, ""date"" : ISODate(""2021-02-03T09:00:00Z"") }" ) ,
90+ BsonDocument . Parse ( @"{ ""_id"" : 3, ""item"" : ""xyz"", ""price"" : 5, ""quantity"" : 5, ""date"" : ISODate(""2021-02-03T09:05:00Z"") }" ) ,
91+ BsonDocument . Parse ( @"{ ""_id"" : 4, ""item"" : ""abc"", ""price"" : 10, ""quantity"" : 10, ""date"" : ISODate(""2021-02-15T08:00:00Z"") }" ) ,
92+ BsonDocument . Parse ( @"{ ""_id"" : 5, ""item"" : ""xyz"", ""price"" : 5, ""quantity"" : 10, ""date"" : ISODate(""2021-02-15T09:05:00Z"") }" ) ,
93+ BsonDocument . Parse ( @"{ ""_id"" : 6, ""item"" : ""xyz"", ""price"" : 5, ""quantity"" : 5, ""date"" : ISODate(""2021-02-15T12:05:10Z"") }" ) ,
94+ BsonDocument . Parse ( @"{ ""_id"" : 7, ""item"" : ""xyz"", ""price"" : 5, ""quantity"" : 10, ""date"" : ISODate(""2021-02-15T14:12:12Z"") }" ) ,
95+ BsonDocument . Parse ( @"{ ""_id"" : 8, ""item"" : ""abc"", ""price"" : 10, ""quantity"" : 5, ""date"" : ISODate(""2021-03-16T20:20:13Z"") }" )
96+ } ) ;
97+
98+ // 2. The response from the server when running count using a strict client:
99+ try
100+ {
101+ #pragma warning disable CS0618 // Type or member is obsolete
102+ collection . Count ( FilterDefinition < BsonDocument > . Empty ) ;
103+ #pragma warning restore CS0618 // Type or member is obsolete
104+ }
105+ catch ( MongoCommandException ex )
106+ {
107+ Console . WriteLine ( ex . Code ) ; // 323
108+ Console . WriteLine ( ex . CodeName ) ; // APIStrictError
109+ Console . WriteLine ( ex . Message ) ; // Command count failed: Provided apiStrict:true, but the command count is not in API Version 1. Information on supported commands and migrations in API Version 1 can be found at https://dochub.mongodb.org/core/manual-versioned-api.
110+ }
111+
112+ // 3. An alternative, accepted command to count the number of documents:
113+ var count = collection . CountDocuments ( FilterDefinition < BsonDocument > . Empty ) ;
114+
115+ // 4. The output of the above command:
116+ Console . WriteLine ( count ) ; // 8
117+ }
63118 }
64119}
0 commit comments