|
16 | 16 | using System;
|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | using System.Linq;
|
19 |
| -using System.Text; |
20 |
| - |
| 19 | +using System.Linq.Expressions; |
21 | 20 | using MongoDB.Bson;
|
22 | 21 | using MongoDB.Bson.IO;
|
23 | 22 | using MongoDB.Bson.Serialization;
|
24 |
| -using MongoDB.Driver; |
| 23 | +using MongoDB.Driver.Linq.Utils; |
25 | 24 |
|
26 | 25 | namespace MongoDB.Driver.Builders
|
27 | 26 | {
|
@@ -177,4 +176,189 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
|
177 | 176 | ((IBsonSerializable)_document).Serialize(bsonWriter, nominalType, options);
|
178 | 177 | }
|
179 | 178 | }
|
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// A builder for specifying which fields of a document the server should return. |
| 182 | + /// </summary> |
| 183 | + /// <typeparam name="TDocument">The type of the document.</typeparam> |
| 184 | + public static class Fields<TDocument> |
| 185 | + { |
| 186 | + // public static properties |
| 187 | + /// <summary> |
| 188 | + /// Gets a null value with a type of IMongoFields. |
| 189 | + /// </summary> |
| 190 | + public static IMongoFields Null |
| 191 | + { |
| 192 | + get { return null; } |
| 193 | + } |
| 194 | + |
| 195 | + // public static methods |
| 196 | + /// <summary> |
| 197 | + /// Adds one or more field names to be excluded from the results. |
| 198 | + /// </summary> |
| 199 | + /// <param name="memberExpressions">One or more field names.</param> |
| 200 | + /// <returns> |
| 201 | + /// The builder (so method calls can be chained). |
| 202 | + /// </returns> |
| 203 | + public static FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions) |
| 204 | + { |
| 205 | + return new FieldsBuilder<TDocument>().Exclude(memberExpressions); |
| 206 | + } |
| 207 | + |
| 208 | + /// <summary> |
| 209 | + /// Adds one or more field names to be included in the results. |
| 210 | + /// </summary> |
| 211 | + /// <param name="memberExpressions">The member expressions.</param> |
| 212 | + /// <returns> |
| 213 | + /// The builder (so method calls can be chained). |
| 214 | + /// </returns> |
| 215 | + public static FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions) |
| 216 | + { |
| 217 | + return new FieldsBuilder<TDocument>().Include(memberExpressions); |
| 218 | + } |
| 219 | + |
| 220 | + /// <summary> |
| 221 | + /// Adds a slice to be included in the results. |
| 222 | + /// </summary> |
| 223 | + /// <typeparam name="TMember">The type of the member.</typeparam> |
| 224 | + /// <param name="memberExpression">The member expression.</param> |
| 225 | + /// <param name="size">The size of the slice (negative sizes are taken from the end).</param> |
| 226 | + /// <returns> |
| 227 | + /// The builder (so method calls can be chained). |
| 228 | + /// </returns> |
| 229 | + public static FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int size) |
| 230 | + { |
| 231 | + return new FieldsBuilder<TDocument>().Slice(memberExpression, size); |
| 232 | + } |
| 233 | + |
| 234 | + /// <summary> |
| 235 | + /// Adds a slice to be included in the results. |
| 236 | + /// </summary> |
| 237 | + /// <typeparam name="TMember">The type of the member.</typeparam> |
| 238 | + /// <param name="memberExpression">The member expression.</param> |
| 239 | + /// <param name="skip">The number of values to skip.</param> |
| 240 | + /// <param name="limit">The number of values to extract.</param> |
| 241 | + /// <returns> |
| 242 | + /// The builder (so method calls can be chained). |
| 243 | + /// </returns> |
| 244 | + public static FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int skip, int limit) |
| 245 | + { |
| 246 | + return new FieldsBuilder<TDocument>().Slice(memberExpression, skip, limit); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /// <summary> |
| 251 | + /// A builder for specifying which fields of a document the server should return. |
| 252 | + /// </summary> |
| 253 | + /// <typeparam name="TDocument">The type of the document.</typeparam> |
| 254 | + [Serializable] |
| 255 | + public class FieldsBuilder<TDocument> : BuilderBase, IMongoFields |
| 256 | + { |
| 257 | + private readonly BsonSerializationInfoHelper _serializationHelper; |
| 258 | + private FieldsBuilder _fieldsBuilder; |
| 259 | + |
| 260 | + // constructors |
| 261 | + /// <summary> |
| 262 | + /// Initializes a new instance of the FieldsBuilder class. |
| 263 | + /// </summary> |
| 264 | + public FieldsBuilder() |
| 265 | + { |
| 266 | + _serializationHelper = new BsonSerializationInfoHelper(); |
| 267 | + _fieldsBuilder = new FieldsBuilder(); |
| 268 | + } |
| 269 | + |
| 270 | + // public methods |
| 271 | + /// <summary> |
| 272 | + /// Adds one or more field names to be excluded from the results. |
| 273 | + /// </summary> |
| 274 | + /// <param name="memberExpressions">The member expressions.</param> |
| 275 | + /// <returns> |
| 276 | + /// The builder (so method calls can be chained). |
| 277 | + /// </returns> |
| 278 | + public FieldsBuilder<TDocument> Exclude(params Expression<Func<TDocument, object>>[] memberExpressions) |
| 279 | + { |
| 280 | + var elementNames = GetElementNames(memberExpressions); |
| 281 | + |
| 282 | + _fieldsBuilder = _fieldsBuilder.Exclude(elementNames.ToArray()); |
| 283 | + return this; |
| 284 | + } |
| 285 | + |
| 286 | + /// <summary> |
| 287 | + /// Adds one or more field names to be included in the results. |
| 288 | + /// </summary> |
| 289 | + /// <param name="memberExpressions">The member expressions.</param> |
| 290 | + /// <returns> |
| 291 | + /// The builder (so method calls can be chained). |
| 292 | + /// </returns> |
| 293 | + public FieldsBuilder<TDocument> Include(params Expression<Func<TDocument, object>>[] memberExpressions) |
| 294 | + { |
| 295 | + var elementNames = GetElementNames(memberExpressions); |
| 296 | + |
| 297 | + _fieldsBuilder = _fieldsBuilder.Include(elementNames.ToArray()); |
| 298 | + return this; |
| 299 | + } |
| 300 | + |
| 301 | + /// <summary> |
| 302 | + /// Adds a slice to be included in the results. |
| 303 | + /// </summary> |
| 304 | + /// <typeparam name="TMember">The type of the member.</typeparam> |
| 305 | + /// <param name="memberExpression">The member expression.</param> |
| 306 | + /// <param name="size">The size of the slice (negative sizes are taken from the end).</param> |
| 307 | + /// <returns> |
| 308 | + /// The builder (so method calls can be chained). |
| 309 | + /// </returns> |
| 310 | + public FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int size) |
| 311 | + { |
| 312 | + var info = _serializationHelper.GetSerializationInfo(memberExpression); |
| 313 | + _fieldsBuilder = _fieldsBuilder.Slice(info.ElementName, size); |
| 314 | + return this; |
| 315 | + } |
| 316 | + |
| 317 | + /// <summary> |
| 318 | + /// Adds a slice to be included in the results. |
| 319 | + /// </summary> |
| 320 | + /// <typeparam name="TMember">The type of the member.</typeparam> |
| 321 | + /// <param name="memberExpression">The member expression.</param> |
| 322 | + /// <param name="skip">The number of values to skip.</param> |
| 323 | + /// <param name="limit">The number of values to extract.</param> |
| 324 | + /// <returns> |
| 325 | + /// The builder (so method calls can be chained). |
| 326 | + /// </returns> |
| 327 | + public FieldsBuilder<TDocument> Slice<TMember>(Expression<Func<TDocument, IEnumerable<TMember>>> memberExpression, int skip, int limit) |
| 328 | + { |
| 329 | + var info = _serializationHelper.GetSerializationInfo(memberExpression); |
| 330 | + _fieldsBuilder = _fieldsBuilder.Slice(info.ElementName, skip, limit); |
| 331 | + return this; |
| 332 | + } |
| 333 | + |
| 334 | + /// <summary> |
| 335 | + /// Converts this object to a BsonDocument. |
| 336 | + /// </summary> |
| 337 | + /// <returns> |
| 338 | + /// A BsonDocument. |
| 339 | + /// </returns> |
| 340 | + public override BsonDocument ToBsonDocument() |
| 341 | + { |
| 342 | + return _fieldsBuilder.ToBsonDocument(); |
| 343 | + } |
| 344 | + |
| 345 | + /// <summary> |
| 346 | + /// Serializes the result of the builder to a BsonWriter. |
| 347 | + /// </summary> |
| 348 | + /// <param name="bsonWriter">The writer.</param> |
| 349 | + /// <param name="nominalType">The nominal type.</param> |
| 350 | + /// <param name="options">The serialization options.</param> |
| 351 | + protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options) |
| 352 | + { |
| 353 | + ((IBsonSerializable)_fieldsBuilder).Serialize(bsonWriter, nominalType, options); |
| 354 | + } |
| 355 | + |
| 356 | + private IEnumerable<string> GetElementNames(IEnumerable<Expression<Func<TDocument, object>>> memberExpressions) |
| 357 | + { |
| 358 | + var elementNames = memberExpressions |
| 359 | + .Select(x => _serializationHelper.GetSerializationInfo(x)) |
| 360 | + .Select(x => x.ElementName); |
| 361 | + return elementNames; |
| 362 | + } |
| 363 | + } |
180 | 364 | }
|
0 commit comments