You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/reference/content/getting_started/reading_and_writing.md
+5-2Lines changed: 5 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,13 +65,16 @@ var result = await collection.UpdateOneAsync(
65
65
66
66
This will generate a filter of `{ Name: "Tom" }` and an update specification of `{ $set: { Profession: "Musician" } }`. Only one document will get updated even if there is more than one person named "Tom" because we used [`UpdateOneAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_UpdateOneAsync" >}}) instead of [`UpdateManyAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_UpdateManyAsync" >}}). More information on updates is available in the [reference guide]({{< relref "reference\driver\crud\writing.md#update-and-replace" >}}).
67
67
68
-
Alternatively, if we want to replace a document completely, we can use the [`ReplaceOneAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_ReplaceOneAsync" >}}) method. Assuming Tom's `Id` value is "550c4aa98e59471bddf68eef":
68
+
Alternatively, if we want to replace a document completely, we can use the [`ReplaceOneAsync`]({{< apiref "M_MongoDB_Driver_IMongoCollection_1_ReplaceOneAsync" >}}) method. Assuming Tom's `Id` value is `550c4aa98e59471bddf68eef`:
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/bson/bson.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,12 +92,12 @@ using (var writer = new JsonWriter(output))
92
92
93
93
[`JsonWriter`]({{< apiref "T_MongoDB_Bson_IO_JsonWriter" >}}) supports writing strict JSON as well as both flavors of [MongoDB Extended JSON]({{< docsref "reference/mongodb-extended-json/" >}}). This, and other things, can be customized with the [`JsonWriterSettings`]({{< apiref "T_MongoDB_Bson_IO_JsonWriterSettings" >}}) class.
94
94
95
-
For instance, to write in a format for the [MongoDB Shell](http://docs.mongodb.org/manual/administration/scripting/), you can set the [`OutputMode`]({{< apiref "M_MongoDB_Bson_IO_JsonWriterSettings_OutputMode" >}}) to `Shell` and also set the [`Version`]({{< apiref "M_MongoDB_Bson_IO_JsonWriterSettings_Version" >}}) to the desired shell version.
95
+
For instance, to write in a format for the [MongoDB Shell](http://docs.mongodb.org/manual/administration/scripting/), you can set the [`OutputMode`]({{< apiref "P_MongoDB_Bson_IO_JsonWriterSettings_OutputMode" >}}) to `Shell` and also set the [`ShellVersion`]({{< apiref "P_MongoDB_Bson_IO_JsonWriterSettings_ShellVersion" >}}) to the desired shell version.
96
96
97
97
```csharp
98
98
varsettings=newJsonWriterSettings
99
99
{
100
100
OutputMode=JsonOutputMode.Shell,
101
-
Version=newVersion(3.0) // target the syntax of MongoDB 3.0
101
+
ShellVersion=newVersion(3.0) // target the syntax of MongoDB 3.0
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/bson/mapping/index.md
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,15 +19,15 @@ The .NET BSON library supports mapping these classes to and from BSON/JSON using
19
19
20
20
In a majority of cases, the driver will be able to automatically map your class for you. This will happen if you begin to use a class for which no [serializer]({{< relref "reference\bson\serialization.md#serializers" >}}) has yet been registered in the [serializer registry]({{< relref "reference\bson\serialization.md#serializer-registry" >}}).
21
21
22
-
You can choose to register the class map using the [`RegisterClassMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_RegisterClassMap_1" >}}) method:
22
+
You can choose to register the class map using the [`RegisterClassMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_RegisterClassMap__1_1" >}}) method:
23
23
24
24
```csharp
25
25
BsonClassMap.RegisterClassMap<MyClass>();
26
26
```
27
27
28
28
{{% note %}}It is very important that the registration of class maps occur prior to them being needed. The best place to register them is at app startup prior to initializing a connection with MongoDB.{{% /note %}}
29
29
30
-
If you want to control the creation of the class map, you can provide your own initializatzion code in the form of a lambda expression:
30
+
If you want to control the creation of the class map, you can provide your own initialization code in the form of a lambda expression:
When your lambda expression is executed, the `cm` (short for class map) parameter is passed an empty class map for you to fill in. In this example, two properties are added to the class map by calling the [`MapMember`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_MapMember_1" >}}) method. The arguments to the method are themselves lambda expressions which identify the member of the class. The advantage of using a lambda expression instead of just a string parameter with the name of the property is that Intellisense and compile time checking ensure that you can’t misspell the name of the property.
40
+
When your lambda expression is executed, the `cm` (short for class map) parameter is passed an empty class map for you to fill in. In this example, two properties are added to the class map by calling the [`MapMember`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_MapMember__1" >}}) method. The arguments to the method are themselves lambda expressions which identify the member of the class. The advantage of using a lambda expression instead of just a string parameter with the name of the property is that Intellisense and compile time checking ensure that you can’t misspell the name of the property.
41
41
42
42
## AutoMap
43
43
44
-
It is also possible to use automapping and then override some of the results using the [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_AutoMap" >}}) method. This method should be called first in the lambda expression.
44
+
It is also possible to use automapping and then override some of the results using the [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_AutoMap" >}}) method. This method should be called first in the lambda expression.
[`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_AutoMap" >}}) uses conventions to map the class and its members. See the [convention documentation]({{< relref "reference\bson\mapping\conventions.md" >}}) for more information.
55
+
[`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_AutoMap" >}}) uses conventions to map the class and its members. See the [convention documentation]({{< relref "reference\bson\mapping\conventions.md" >}}) for more information.
You can design your class to be capable of handling any extra elements that might be found in a BSON document during deserialization. To do so, you must have a property of type [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}) (or [`IDictionary<string, object>`]({{< msdnref "" >}})) and you must identify that property as the one that should hold any extra elements that are found. By [convention]({{< relref "reference\bson\mapping\conventions.md" >}}), the member may be named "ExtraElements". For example:
90
+
You can design your class to be capable of handling any extra elements that might be found in a BSON document during deserialization. To do so, you must have a property of type [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}) (or [`IDictionary<string, object>`]({{< msdnref "s4ys34ea" >}})) and you must identify that property as the one that should hold any extra elements that are found. By [convention]({{< relref "reference\bson\mapping\conventions.md" >}}), the member may be named `ExtraElements`. For example:
When you Insert a document, the driver checks to see if the `Id` member has been assigned a value and, if not, generates a new unique value for it. Since the `Id` member can be of any type, the driver requires the help of an [`IIdGenerator`]({{< apiref "T_MongoDB_Bson_Serialization_IIdGenerator" >}}) to check whether the `Id` has a value assigned to it and to generate a new value if necessary. The driver has the following Id generators built-in:
Some of these Id generators are used automatically for commonly used `Id` types:
334
334
335
-
-[`GuidGenerator`]({{ apiref "T_MongoDB_Bson_Serialization_IdGenerators_GuidGenerator" >}}) is used for a [`Guid`]({{< msdnref "system.guid" >}})
336
-
-[`ObjectIdGenerator`]({{ apiref "T_MongoDB_Bson_Serialization_IdGenerators_ObjectIdGenerator" >}}) is used for an [`ObjectId`]({{< apiref "T_MongoDB_Bson_ObjectId" >}})
337
-
-[`StringObjectIdGenerator`]({{ apiref "T_MongoDB_Bson_Serialization_IdGenerators_StringObjectIdGenerator" >}}) is used for a [`string`]({{< msdnref "system.string" >}}) represented externally as [`ObjectId`]({{< apiref "T_MongoDB_Bson_ObjectId" >}})
335
+
-[`GuidGenerator`]({{< apiref "T_MongoDB_Bson_Serialization_IdGenerators_GuidGenerator" >}}) is used for a [`Guid`]({{< msdnref "system.guid" >}})
336
+
-[`ObjectIdGenerator`]({{< apiref "T_MongoDB_Bson_Serialization_IdGenerators_ObjectIdGenerator" >}}) is used for an [`ObjectId`]({{< apiref "T_MongoDB_Bson_ObjectId" >}})
337
+
-[`StringObjectIdGenerator`]({{< apiref "T_MongoDB_Bson_Serialization_IdGenerators_StringObjectIdGenerator" >}}) is used for a [`string`]({{< msdnref "system.string" >}}) represented externally as [`ObjectId`]({{< apiref "T_MongoDB_Bson_ObjectId" >}})
You could also say that you want to use the [`CombGuidGenerator`]({{ apiref "T_MongoDB_Bson_Serialization_IdGenerators_CombGuidGenerator" >}}) for all Guids.
359
+
You could also say that you want to use the [`CombGuidGenerator`]({{< apiref "T_MongoDB_Bson_Serialization_IdGenerators_CombGuidGenerator" >}}) for all Guids.
When using code, [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_AutoMap" >}}) will have initially added the property to the class map automatically. [`UnmapMember`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_UnmapMember_1" >}}) will remove it.
391
+
When using code, [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_AutoMap" >}}) will have initially added the property to the class map automatically. [`UnmapMember`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_UnmapMember__1" >}}) will remove it.
392
392
393
393
394
394
### Ignoring Default Values
@@ -448,7 +448,7 @@ public class MyClass
448
448
449
449
### Ignoring a Member at Runtime
450
450
451
-
Sometimes the decision whether to serialize a member or not is more complicated than just whether the value is `null` or equal to the default value. In these cases, you can write a method that determines whether a value should be serialized. Usually the method for member Xyz is named ShouldSerializeXyz. If you follow this naming convention then [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_1_AutoMap" >}}) will automatically detect the method and use it. For example:
451
+
Sometimes the decision whether to serialize a member or not is more complicated than just whether the value is `null` or equal to the default value. In these cases, you can write a method that determines whether a value should be serialized. Usually the method for member Xyz is named ShouldSerializeXyz. If you follow this naming convention then [`AutoMap`]({{< apiref "M_MongoDB_Bson_Serialization_BsonClassMap_AutoMap" >}}) will automatically detect the method and use it. For example:
452
452
453
453
```csharp
454
454
publicclassEmployee
@@ -551,7 +551,7 @@ public class C
551
551
}
552
552
```
553
553
554
-
When done via code, a [`DictionaryInterfaceImplementerSerializer`]({{< apiref "T_MongoDB_Bson_Serialization_Serializers_DictionaryInterfaceImplementerSerializer" >}}) should be set:
554
+
When done via code, a [`DictionaryInterfaceImplementerSerializer`]({{< apiref "T_MongoDB_Bson_Serialization_Serializers_DictionaryInterfaceImplementerSerializer_1" >}}) should be set:
It is possible to implement custom attributes to contribute to the serialization infrastructure. There are 3 interfaces you might want to implement:
623
623
624
-
-[`IBsonClassMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_Attributes_IBsonClassMapAttribute" >}}) is used to contribute to a class map.
625
-
-[`IBsonMemberMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_Attributes_IBsonMemberMapAttribute" >}}) is used to contribute to a member map.
626
-
-[`IBsonCreatorMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_Attributes_IBsonCreatorMapAttribute" >}}) is used to contribute to a creator map.
624
+
-[`IBsonClassMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonClassMapAttribute" >}}) is used to contribute to a class map.
625
+
-[`IBsonMemberMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonMemberMapAttribute" >}}) is used to contribute to a member map.
626
+
-[`IBsonCreatorMapAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonCreatorMapAttribute" >}}) is used to contribute to a creator map.
627
627
628
628
All the provided attributes implement one or more of these interfaces, so they are good examples of how these interfaces function.
Copy file name to clipboardExpand all lines: Docs/reference/content/reference/bson/serialization.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ Serialization is the process of mapping an object to and from a BSON document. T
16
16
17
17
## Serializer Registry
18
18
19
-
The serializer registry contains all the [`IBsonSerializers`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializer" >}}) that have been registered. It can be accessed via the [`SerializerRegistry`]({{< apiref "P_MongoDB_Bson_BsonSerializer_SerializerRegistry" >}}) property of the static class [`BsonSerializer`]({{< apiref "MongoDB_Bson_BsonSerializer" >}}).
19
+
The serializer registry contains all the [`IBsonSerializers`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializer" >}}) that have been registered. It can be accessed via the [`SerializerRegistry`]({{< apiref "P_MongoDB_Bson_Serialization_BsonSerializer_SerializerRegistry" >}}) property of the static class [`BsonSerializer`]({{< apiref "T_MongoDB_Bson_Serialization_BsonSerializer" >}}).
20
20
21
21
{{% note %}}This is a global registry. Currently, you cannot use multiple registries in a single application.{{% /note %}}
22
22
@@ -28,7 +28,7 @@ The [serializer registry]({{< relref "#serializer-registry" >}}) is powered by a
28
28
29
29
### Implementation
30
30
31
-
To implement an [`IBsonSerializationProvider`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializationProvider" >}}), create a class that implements the interface and register it using the [`RegisterSerializationProvider`]({{< apiref "T_MongoDB_Bson_BsonSerializer_RegisterSerializationProvider" >}}) method.
31
+
To implement an [`IBsonSerializationProvider`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializationProvider" >}}), create a class that implements the interface and register it using the [`RegisterSerializationProvider`]({{< apiref "M_MongoDB_Bson_Serialization_BsonSerializer_RegisterSerializationProvider" >}}) method.
32
32
33
33
```csharp
34
34
classMyProvider : IBsonSerializationProvider
@@ -67,7 +67,7 @@ using (var reader = new JsonReader(jsonString))
67
67
68
68
{{% note class="warning" %}}Writing custom serializers to handle both normal cases and edge cases can be very tricky.{{% /note %}}
69
69
70
-
To implement a custom [`IBsonSerializer`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializer" >}}), it is best to inherit from [`SerializerBase<T>`]({{< apiref "T_MongoDB_Bson_Serialization_Serializers_SerializerBase_1" >}}) and override the [`Deserializer`]({{< apiref "M_MongoDB_Bson_Serialization_Serializers_SerializerBase_1_Deserialize" >}}) and [`Serialize`]({{< apiref "M_MongoDB_Bson_Serialization_Serializers_SerializerBase_1_Serialize" >}}) methods.
70
+
To implement a custom [`IBsonSerializer`]({{< apiref "T_MongoDB_Bson_Serialization_IBsonSerializer" >}}), it is best to inherit from [`SerializerBase<T>`]({{< apiref "T_MongoDB_Bson_Serialization_Serializers_SerializerBase_1" >}}) and override the [`Deserialize`]({{< apiref "M_MongoDB_Bson_Serialization_Serializers_SerializerBase_1_Deserialize" >}}) and [`Serialize`]({{< apiref "M_MongoDB_Bson_Serialization_Serializers_SerializerBase_1_Serialize" >}}) methods.
71
71
72
72
For example, it implement a serializer that reads an [`Int32`]({{< msdnref "system.int32" >}}):
73
73
@@ -122,7 +122,7 @@ Notice that we are testing the current BsonType while reading and making decisio
122
122
123
123
{{% note %}}The built-in [`Int32Serializer`]({{< apiref "T_MongoDB_Bson_Serialization_Serializers_Int32Serializer" >}}) accounts for this as well as other such items.{{% /note %}}
124
124
125
-
You can register your serializer using the [`RegisterSerializer`]({{< apiref "T_MongoDB_Bson_BsonSerializer_RegisterSerializer" >}}) or implement a [serialization provider]({{< relref "#serialization-provider" >}}).
125
+
You can register your serializer using the [`RegisterSerializer`]({{< apiref "M_MongoDB_Bson_Serialization_BsonSerializer_RegisterSerializer" >}}) or implement a [serialization provider]({{< relref "#serialization-provider" >}}).
0 commit comments