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/reference/driver/definitions.md
+61-84Lines changed: 61 additions & 84 deletions
Original file line number
Diff line number
Diff line change
@@ -11,107 +11,75 @@ title = "Definitions and Builders"
11
11
12
12
## Definitions and Builders
13
13
14
-
The driver has introduced a number of types related to the specification of filters, updates, projections, sorts, etc... These types are used throughout the [API]({{< relref "reference\driver\crud\index.md" >}}). In addition, most of them have builders as well to aid in their creation.
14
+
The driver has introduced a number of types related to the specification of filters, updates, projections, sorts, and index keys. These types are used throughout the [API]({{< relref "reference\driver\crud\index.md" >}}).
15
15
16
+
Most of the definitions also have builders to aid in their creation. Each builder has a generic type parameter `TDocument` which represents the type of document with which you are working. It will almost always match the generic `TDocument` parameter used in an [`IMongoCollection<TDocument>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}).
16
17
17
-
## Fields
18
18
19
-
[`FieldDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_1" >}}) and [`FieldDefinition<TDocument, TField>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_2" >}}) define how to get a field name. They are implicitly convertible from a string, so that you can simply pass the field name you'd like.
19
+
## Fields
20
20
21
-
For instance, with a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}), the following will be true:
21
+
[`FieldDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_1" >}}) and [`FieldDefinition<TDocument, TField>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_2" >}}) define how to get a field name. They are implicitly convertible from a string, so that you can simply pass the field name you'd like. For instance, to use the field named "fn" with a `TDocument` of [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}), do the following:
However, if you are working with a [mapped class]({{< relref "reference\bson\mapping\index.md" >}}), then 2 further options exist.
34
-
35
-
First, most locations that accept a [`FieldDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_1" >}}) or [`FieldDefinition<TDocument, TField>`]({{< apiref "T_MongoDB_Driver_FieldDefinition_2" >}}) will also have an overload (via extension method) that accepts a lambda expression. Given the following class definition marked with the [`BsonElementAttribute`]({{< apiref "T_MongoDB_Bson_Serialization_Attributes_BsonElementAttribute" >}}) instructing the use of a different field name:
27
+
However, if you are working with a [mapped class]({{< relref "reference\bson\mapping\index.md" >}}), then we are able to translate a string that equals the property name. For instance, given the below `Person` class:
36
28
37
29
```csharp
38
30
classPerson
39
31
{
40
32
[BsonElement("fn")]
41
33
publicstringFirstName { get; set; }
42
34
43
-
[BsonElement("fn")]
35
+
[BsonElement("ln")]
44
36
publicstringLastName { get; set; }
45
37
}
46
38
```
47
39
48
-
Then the following will be true.
40
+
Since we know the type is `Person`, we can provide the property name, `FirstName`, from the class and "fn" will still be used.
In the same way, since we know the type of document, `Person` in this case, we can also translate fields names provided as a string.
45
+
{{% note %}}We don't validate that the provided string exists as a mapped field, so it is still possible to provide a field that hasn't been mapped:{{% /note %}}
And the output field name of this will be just "fn".
83
52
84
53
85
54
## Filters
86
55
87
-
[`FilterDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_FilterDefinition_1" >}}) defines a filter. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
56
+
[`FilterDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_FilterDefinition_1" >}}) defines a filter. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}).
_See the [tests]({{< srcref "MongoDB.Driver.Tests/FilterDefinitionBuilderTests.cs" >}}) for examples._
104
72
105
-
The [`FilterDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_FilterDefinitionBuilder_1" >}}) provides a nice API for building up both simple and complex [MongoDB queries]({{< docsref "reference/operator/query/" >}}).
73
+
The [`FilterDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_FilterDefinitionBuilder_1" >}}) provides a type-safe API for building up both simple and complex [MongoDB queries]({{< docsref "reference/operator/query/" >}}).
106
74
107
-
For example, to build up the filter `{ x: 10, y: { $lt: 20 } }`, the following calls are all comparable.
75
+
For example, to build up the filter `{ x: 10, y: { $lt: 20 } }`, the following calls are all equivalent.
{{% note %}}Note that the`&` operator is overloaded. Other overloaded operators include the `|` operator for "or" and the `!` operator for "not".{{% /note %}}
82
+
{{% note %}}The`&` operator is overloaded. Other overloaded operators include the `|` operator for "or" and the `!` operator for "not".{{% /note %}}
115
83
116
84
Given the following class:
117
85
@@ -126,27 +94,25 @@ class Widget
126
94
}
127
95
```
128
96
129
-
We can achieve the same result in the typed variant:
97
+
You can achieve the same result in the typed variant:
For more information on valid lambda expressions, see the [expressions documentation]({{< relref "reference\driver\expressions.md" >}}).
151
117
152
118
#### Array Operators
@@ -190,13 +156,26 @@ PipelineDefinition pipeline = new BsonDocument[]
190
156
191
157
## Projections
192
158
193
-
There are two forms of a projection definition, one where the type of the projection is known ([`ProjectionDefinition<TDocument, TProjection>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinition_2" >}})) and one where the type of the projection is not yet known ([`ProjectionDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinition_1" >}})). The latter, while implicitly convertible to the first, is merely used as a building block. The [high-level APIs]({{< relref "reference\driver\crud\index.md" >}}) that take a projection will always take the former. This is because, when determining how to handle a projection client-side, it is not enough to know what fields and transformations will take place. It also requires that we know how to interpret the projected shape as a .NET type. Since the driver allows you to work with custom classes, it is imperative that any projection also include the "interpretation instructions" for projecting into a custom class.
159
+
There are two forms of a projection definition: one where the type of the projection is known, [`ProjectionDefinition<TDocument, TProjection>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinition_2" >}}), and one where the type of the projection is not yet known, [`ProjectionDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinition_1" >}}). The latter, while implicitly convertible to the first, is merely used as a building block. The [high-level APIs]({{< relref "reference\driver\crud\index.md" >}}) that take a projection will always take the former. This is because, when determining how to handle a projection client-side, it is not enough to know what fields and transformations will take place. It also requires that we know how to interpret the projected shape as a .NET type. Since the driver allows you to work with custom classes, it is imperative that any projection also include the "interpretation instructions" for projecting into a custom class.
160
+
161
+
Each projection definition is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}).
Both of these will render the projection `{ x: 1 }`.
172
+
194
173
195
174
### Projection Definition Builder
196
175
197
176
_See the [tests]({{< srcref "MongoDB.Driver.Tests/ProjectionDefinitionBuilderTests.cs" >}}) for examples._
198
177
199
-
The [`ProjectionDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinitionBuilder_1" >}}) exists to make it easier to build up projections in MongoDB's syntax. To render the projection `{ x: 1, y: 1, _id: 0 }`, the following should be done:
178
+
The [`ProjectionDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_ProjectionDefinitionBuilder_1" >}}) exists to make it easier to build up projections in MongoDB's syntax. For the projection `{ x: 1, y: 1, _id: 0 }`:
@@ -295,25 +274,23 @@ A projection is also used when performing grouping in the [Aggregation Framework
295
274
296
275
## Sorts
297
276
298
-
[`SortDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_SortDefinition_1" >}}) defines how to render a valid sort document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
277
+
[`SortDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_SortDefinition_1" >}}) defines how to render a valid sort document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}).
_See the [tests]({{< srcref "MongoDB.Driver.Tests/SortDefinitionBuilderTests.cs" >}}) for examples._
315
292
316
-
The [`SortDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_SortDefinitionBuilder_1" >}}) provides a nice API for building up [MongoDB sort syntax]({{< docsref "reference/method/cursor.sort/" >}}).
293
+
The [`SortDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_SortDefinitionBuilder_1" >}}) provides a type-safe API for building up [MongoDB sort syntax]({{< docsref "reference/method/cursor.sort/" >}}).
317
294
318
295
For example, to build up the sort `{ x: 1, y: -1 }`, do the following:
319
296
@@ -353,25 +330,25 @@ var sort = builder.Ascending("x").Descending("y");
353
330
354
331
## Updates
355
332
356
-
[`UpdateDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinition_1" >}}) defines how to render a valid update document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
333
+
[`UpdateDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinition_1" >}}) defines how to render a valid update document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}).
Both of these will render the update `{ $set: { x: 1 } }`.
345
+
346
+
370
347
### Update Definition Builder
371
348
372
349
_See the [tests]({{< srcref "MongoDB.Driver.Tests/UpdateDefinitionBuilderTests.cs" >}}) for examples._
373
350
374
-
The [`UpdateDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinitionBuilder_1" >}}) provides a nice API for building the [MongoDB update specification]({{< docsref "reference/operator/update/" >}}).
351
+
The [`UpdateDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinitionBuilder_1" >}}) provides a type-safe API for building the [MongoDB update specification]({{< docsref "reference/operator/update/" >}}).
375
352
376
353
For example, to build up the update `{ $set: { x: 1, y: 3 }, $inc: { z: 1 } }`, do the following:
377
354
@@ -400,7 +377,7 @@ We can achieve the same result in a typed variant:
0 commit comments