13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System ;
17
+ using MongoDB . Bson ;
18
+ using Xunit ;
19
+
16
20
namespace MongoDB . Driver . Examples
17
21
{
18
22
public class VersionedApiExamples
19
23
{
24
+ [ Fact ]
20
25
public void ConfigureServerApi ( )
21
26
{
22
27
// Start Versioned API Example 1
@@ -28,6 +33,7 @@ public void ConfigureServerApi()
28
33
// End Versioned API Example 1
29
34
}
30
35
36
+ [ Fact ]
31
37
public void ConfigureServerApiStrict ( )
32
38
{
33
39
// Start Versioned API Example 2
@@ -39,6 +45,7 @@ public void ConfigureServerApiStrict()
39
45
// End Versioned API Example 2
40
46
}
41
47
48
+ [ Fact ]
42
49
public void ConfigureServerApiNonStrict ( )
43
50
{
44
51
// Start Versioned API Example 3
@@ -50,6 +57,7 @@ public void ConfigureServerApiNonStrict()
50
57
// End Versioned API Example 3
51
58
}
52
59
60
+ [ Fact ]
53
61
public void ConfigureServerApiDeprecationErrors ( )
54
62
{
55
63
// Start Versioned API Example 4
@@ -60,5 +68,52 @@ public void ConfigureServerApiDeprecationErrors()
60
68
var mongoClient = new MongoClient ( mongoClientSettings ) ;
61
69
// End Versioned API Example 4
62
70
}
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
+ }
63
118
}
64
119
}
0 commit comments