Skip to content

Commit d10eb06

Browse files
CSHARP-3370: Document Versioned API Usage in Drivers (#458)
1 parent 9dd589c commit d10eb06

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

Docs/reference/content/reference/driver/connecting.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,7 @@ A majority of the methods and extension methods for an [`IMongoCollection<TDocum
127127

128128
### Re-use
129129

130-
The implementation of [`IMongoCollection<TDocument>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}) ultimately provided by a [`MongoClient`]({{< apiref "T_MongoDB_Driver_MongoClient" >}}) is thread-safe and is safe to be stored globally or in an IoC container.
130+
The implementation of [`IMongoCollection<TDocument>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}) ultimately provided by a [`MongoClient`]({{< apiref "T_MongoDB_Driver_MongoClient" >}}) is thread-safe and is safe to be stored globally or in an IoC container.
131+
132+
### Server API
133+
Starting from version 5.0 MongoDB servers support specifying API compatibility version. See [Versioned API]({{< relref "reference\driver\versioned_api.md" >}}).
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
+++
2+
date = "2021-02-12T15:36:56Z"
3+
draft = false
4+
title = "Versioned API"
5+
[menu.main]
6+
parent = "Driver"
7+
identifier = "Versioned API"
8+
weight = 70
9+
pre = "<i class='fa'></i>"
10+
+++
11+
12+
## Versioned API
13+
14+
Versioned API is a new feature in MongoDB 5.0 that allows user-selectable API versions, subsets of MongoDB
15+
server semantics, to be declared on a client. During communication with a server, clients with a declared
16+
API version will force the server to behave in a manner compatible with the API version. Declaring an API
17+
version on a client can be used to ensure consistent responses from a server, providing long term API
18+
stability for an application. The declared API version is applied to all commands run through the client, including those sent through
19+
the generic RunCommand helper. Specifying versioned API options in the command document AND declaring an API version on the client is not supported and will lead to undefined behaviour.
20+
21+
You can specify [`ServerApi`]({{< apiref "T_MongoDB_Driver_Core_ServerApi" >}}) via [`MongoClientSettings`]({{< apiref "T_MongoDB_Driver_MongoClientSettings" >}}):
22+
23+
```csharp
24+
var serverApi = new ServerApi(ServerApiVersion.V1);
25+
var settings = new MongoClientSettings { ServerApi = serverApi };
26+
var client = new MongoClient(settings);
27+
```
28+
29+
The [`ServerApi`]({{< apiref "T_MongoDB_Driver_Core_ServerApi" >}}) can be specified only when creating a [`MongoClient`]({{< apiref "T_MongoDB_Driver_MongoClient" >}}) and cannot be changed during the course of execution. Thus to run any command with a different
30+
API version or without declaring one, create a separate [`MongoClient`]({{< apiref "T_MongoDB_Driver_MongoClient" >}}) that declares the appropriate API version.
31+
32+
The [`ServerApi`]({{< apiref "T_MongoDB_Driver_Core_ServerApi" >}}) consists of 3 fields. One mandatory: `serverApiVersion` and two optional: `strict` and `deprecationErrors`.
33+
34+
### Server API version
35+
36+
The [`ServerApiVersion`]({{< apiref "T_MongoDB_Driver_Core_ServerApiVersion" >}}) is a required parameter of type [`ServerApi`]({{< apiref "T_MongoDB_Driver_Core_ServerApi" >}}) and represents the version number that the server should behave in compatiblity with. Currently only version 1 is available. It can be acquired via a static property:
37+
38+
```csharp
39+
var serverApiVersion = ServerApiVersion.V1;
40+
```
41+
42+
### Strict flag
43+
44+
The `strict` flag is optional and defaults to false. Setting `strict` to true causes commands (or their specific behavior, like command options or aggregation pipeline stages) to fail if they are not part of the declared API version.
45+
46+
```csharp
47+
var serverApi = new ServerApi(ServerApiVersion.V1, strict: true);
48+
var settings = new MongoClientSettings { ServerApi = serverApi };
49+
var client = new MongoClient(settings);
50+
var database = client.GetDatabase("db");
51+
var collection = database.GetCollection<BsonDocument>("coll");
52+
var result = collection.Distinct((FieldDefinition<BsonDocument, int>)"a.b", new BsonDocument("x", 1)); // Fails with:
53+
// MongoDB.Driver.MongoCommandException : Command distinct failed: Provided apiStrict:true, but the command distinct is not in API Version 1.
54+
```
55+
56+
### DeprecationErrors flag
57+
58+
The `deprecationErrors` flag is optional and defaults to false. Setting `deprecationErrors` to true causes commands (or their specific behavior) to fail with an error if they are deprecated in the declared API version.
59+
60+
{{% note %}}Currently there are no deprecations in version 1, so a theoretical example is used.{{% /note %}}
61+
62+
```csharp
63+
var serverApi = new ServerApi(ServerApiVersion.V1, deprecationErrors: true);
64+
var settings = new MongoClientSettings { ServerApi = serverApi };
65+
var client = new MongoClient(settings);
66+
var database = client.GetDatabase("db");
67+
var result = database.RunCommand<BsonDocument>(new BsonDocument("commandDeprecatedInV1", 1)); // Example fail:
68+
// MongoDB.Driver.MongoCommandException : Command commandDeprecatedInV1 failed: Provided deprecationErrors:true, but the command commandDeprecatedInV1 is deprecated in API Version 1.
69+
```

0 commit comments

Comments
 (0)