Skip to content

Commit a3f4323

Browse files
committed
added Migration section to the Upgrading notes.
1 parent e7f600e commit a3f4323

File tree

1 file changed

+133
-6
lines changed

1 file changed

+133
-6
lines changed

Docs/reference/content/upgrading.md

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@ title = "Upgrading"
88
pre = "<i class='fa'></i>"
99
+++
1010

11-
## Upgrading
11+
## Breaking Changes
1212

1313
As 2.0 is a major revision, there are some breaking changes when coming from the 1.x assemblies. We've tried our best to mitigate those breaking changes, but some were inevitable. These changes may not affect everyone, but take a moment to review the list of known changes below:
1414

15-
16-
## System Requirements
15+
### System Requirements
1716

1817
- .NET 3.5 and .NET 4.0 are no longer supported. If you still must use these platforms, the 1.x series of the driver will continue to be developed.
1918
- [CSHARP-952](https://jira.mongodb.org/browse/CSHARP-952): We've removed support for partially trusted callers.
2019

2120

22-
## Packaging
21+
### Packaging
2322

2423
- The nuget package [mongocsharpdriver](http://nuget.org/packages/mongocsharpdriver) now includes the legacy driver. It depends on 3 new nuget packages, [MongoDB.Bson](http://nuget.org/packages/MongoDB.Bson), [MongoDB.Driver.Core](http://nuget.org/packages/MongoDB.Driver.Core), and [MongoDB.Driver](http://nuget.org/packages/MongoDB.Driver). [MongoDB.Driver](http://nuget.org/packages/MongoDB.Driver) is the replacement for [mongocsharpdriver](http://nuget.org/packages/mongocsharpdriver).
2524
- [CSHARP-616](https://jira.mongodb.org/browse/CSHARP-616): We are no longer strong naming our assemblies. Our previous strong naming was signed with a key in our public repository. This did nothing other than satisfy certain tools. If you need MongoDB assemblies to be strongly named, it is relatively straight-forward to build the assemblies yourself.
2625

2726

28-
## BSON
27+
### BSON
2928

3029
- [CSHARP-933](https://jira.mongodb.org/browse/CSHARP-933): Improved the BSON Serializer infrastructure. Anyone who has written a custom serializer will be affected by this. The changes are minor, but were necessary to support dynamic serializers as well as offering great speed improvements and improved memory management.
3130
- Certain methods, such as `BsonMemberMap.SetRepresentation` have been removed. The proper way to set a representation, for instance, would be to use `SetSerializer` and configure the serializer with the appropriate representation.
3231
- [CSHARP-939](https://jira.mongodb.org/browse/CSHARP-939): Dynamic DictionaryRepresentation has been removed. Its intent was to store, in some manner, anything in a .NET dictionary. In practice, this leads to the same values getting stored in different ways depending on factors such as a "." inside the key name. We made the decision to eliminate this variability. This means that documents that used to serialize correctly may start throwing a BsonSerializationException with a message indicating the key must be a valid string. [CSHARP-1165](https://jira.mongodb.org/browse/CSHARP-1165) has a solution to this problem. It should be noted that we will continue to read these disparate representations without error.
3332

3433

35-
## Driver
34+
### Driver
3635
- [CSHARP-979](https://jira.mongodb.org/browse/CSHARP-979): `MongoConnectionStringBuilder` has been removed. Use the documented mongodb connection string format and/or `MongoUrlBuilder`.
3736
- `MongoServer` is a deprecated class. Anyone using `MongoClient.GetServer()` will encounter a deprecation warning and, depending on how your build is setup, may receive an error. It is still safe to use this API until your code is ported to the new API. *Note that this API requires the use of the [mongocsharpdriver](http://nuget.org/packages/mongocsharpdriver) to include the legacy API.
3837
- [CSHARP-1043](https://jira.mongodb.org/browse/CSHARP-1043) and [CSHARP-1044](https://jira.mongodb.org/browse/CSHARP-1044): `ReadPreference` and `WriteConcern` were rewritten. These classes are now immutable. Any current application code that sets values on these classes will no longer function. Instead, you should use the With method to alter a `ReadPreference` or `WriteConcern`.
@@ -41,3 +40,131 @@ As 2.0 is a major revision, there are some breaking changes when coming from the
4140
var writeConcern = myCurrentWriteConcern.With(journal: true);
4241
```
4342

43+
## Migrating
44+
45+
Below are some common actions in the old API and their counterpart in the new API.
46+
47+
### Builders
48+
49+
_[More information.]({{< relref "reference\driver\definitions.md" >}})_
50+
51+
The old builders (Query, Update, etc...) have all be replaced with Builders<T>.Filter, Builders<T>.Update, etc...
52+
53+
```csharp
54+
// old API
55+
var query = Query.And(
56+
Query<Person>.EQ(x => x.FirstName, "Jack"),
57+
Query<Person>.LT(x => x.Age, 21));
58+
59+
// new API
60+
var builder = Builders<Person>.Filter;
61+
var filter = builder.Eq(x => x.FirstName, "Jack") & builder.Lt(x => x.Age, 21);
62+
```
63+
64+
```csharp
65+
// old API
66+
var update = Update.Combine(
67+
Update<Person>.Set(x => x.LastName, "McJack"),
68+
Update<Person>.Inc(x => x.Age, 1));
69+
70+
// new API
71+
var update = Builders<Person>.Update
72+
.Set(x => x.LastName, "McJack")
73+
.Inc(x => x.Age, 1);
74+
```
75+
76+
### Finding All Documents
77+
78+
_[More information.]({{< relref "reference\driver\crud\reading.md#finding-documents" >}})_
79+
80+
To find all documents, you must specify an empty filter.
81+
82+
```csharp
83+
// old API
84+
var list = collection.FindAll().ToList();
85+
86+
// new API
87+
var list = await collection.Find(new BsonDocument()).ToListAsync();
88+
```
89+
90+
### Finding One Document
91+
92+
_[More information.]({{< relref "reference\driver\crud\reading.md#single-results" >}})_
93+
94+
To find all documents, you must specify an empty filter.
95+
96+
```csharp
97+
// old API
98+
var doc = collection.FindOne();
99+
100+
// new API
101+
var doc = await collection.Find(new BsonDocument()).FirstOrDefaultAsync();
102+
```
103+
104+
### Iteration
105+
106+
_[More information.]({{< relref "reference\driver\crud\reading.md#iteration" >}})_
107+
108+
You cannot iterate synchronously using a foreach loop without first getting a list. However, there are alternative methods provided to loop over all the documents in an asynchronous manner.
109+
110+
```csharp
111+
// old API
112+
foreach(var doc in collection.Find(new QueryDocument("Name", "Jack")))
113+
{
114+
// do something
115+
}
116+
117+
// new API
118+
await collection.Find(new BsonDocument("Name", "Jack"))
119+
.ForEachAsync(doc =>
120+
{
121+
// do something
122+
});
123+
124+
await collection.Find(new BsonDocument("Name", "Jack"))
125+
.ForEachAsync(async doc =>
126+
{
127+
// do something with await
128+
});
129+
```
130+
131+
### Counting All Documents
132+
133+
_[More information.]({{< relref "reference\driver\crud\reading.md#counting-documents" >}})_
134+
135+
To find all documents, you must specify an empty filter.
136+
137+
```csharp
138+
// old API
139+
var count = collection.Count();
140+
141+
// new API
142+
var count = await collection.CountAsync(new BsonDocument());
143+
```
144+
145+
### Mapping - SetRepresentation
146+
147+
_[More information.]({{< relref "reference\bson\mapping\index.md#representation" >}})_
148+
149+
You can still use attributes as you have done before. To set the representation via code:
150+
151+
```csharp
152+
class Test
153+
{
154+
public char RepresentAsInt32 { get; set; }
155+
}
156+
157+
// old API
158+
BsonClassMap.RegisterClassMap<Person>(cm =>
159+
{
160+
// snip...
161+
cm.MapMember(x => x.RepresentAsInt32).SetRepresentation(BsonType.Int32);
162+
});
163+
164+
// new API
165+
BsonClassMap.RegisterClassMap<Person>(cm =>
166+
{
167+
// snip...
168+
cm.MapMember(x => x.RepresentAsInt32).SetSerializer(new CharSerializer(BsonType.Int32));
169+
});
170+
```

0 commit comments

Comments
 (0)