|
22 | 22 | using MongoDB.Bson.Serialization;
|
23 | 23 | using MongoDB.Driver.Core.Misc;
|
24 | 24 | using MongoDB.Driver.Linq;
|
| 25 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
25 | 26 | using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
|
26 | 27 | using MongoDB.Driver.Linq.Linq3Implementation.Translators;
|
27 | 28 |
|
@@ -477,6 +478,52 @@ public static PipelineStageDefinition<TInput, TInput> Densify<TInput>(
|
477 | 478 | return Densify(field, range, (IEnumerable<Expression<Func<TInput, object>>>)partitionByFields);
|
478 | 479 | }
|
479 | 480 |
|
| 481 | + /// <summary> |
| 482 | + /// Creates a $documents stage. |
| 483 | + /// </summary> |
| 484 | + /// <typeparam name="TDocument">The type of the documents.</typeparam> |
| 485 | + /// <param name="documents">The documents.</param> |
| 486 | + /// <param name="documentSerializer">The document serializer.</param> |
| 487 | + /// <returns>The stage.</returns> |
| 488 | + public static PipelineStageDefinition<NoPipelineInput, TDocument> Documents<TDocument>( |
| 489 | + AggregateExpressionDefinition<NoPipelineInput, IEnumerable<TDocument>> documents, |
| 490 | + IBsonSerializer<TDocument> documentSerializer = null) |
| 491 | + { |
| 492 | + if (typeof(TDocument) == typeof(NoPipelineInput)) |
| 493 | + { |
| 494 | + throw new ArgumentException("Documents cannot be of type NoPipelineInput.", nameof(documents)); |
| 495 | + } |
| 496 | + |
| 497 | + const string operatorName = "$documents"; |
| 498 | + var stage = new DelegatedPipelineStageDefinition<NoPipelineInput, TDocument>( |
| 499 | + operatorName, |
| 500 | + (s, sr, linqProvider) => |
| 501 | + { |
| 502 | + var renderedDocuments = documents.Render(NoPipelineInputSerializer.Instance, sr, linqProvider); |
| 503 | + return new RenderedPipelineStageDefinition<TDocument>( |
| 504 | + operatorName, |
| 505 | + new BsonDocument(operatorName, renderedDocuments), |
| 506 | + documentSerializer ?? sr.GetSerializer<TDocument>()); |
| 507 | + }); |
| 508 | + |
| 509 | + return stage; |
| 510 | + } |
| 511 | + |
| 512 | + /// <summary> |
| 513 | + /// Creates a $documents stage. |
| 514 | + /// </summary> |
| 515 | + /// <typeparam name="TDocument">The type of the documents.</typeparam> |
| 516 | + /// <param name="documents">The documents.</param> |
| 517 | + /// <param name="documentSerializer">The document serializer.</param> |
| 518 | + /// <returns>The stage.</returns> |
| 519 | + public static PipelineStageDefinition<NoPipelineInput, TDocument> Documents<TDocument>( |
| 520 | + IEnumerable<TDocument> documents, |
| 521 | + IBsonSerializer<TDocument> documentSerializer = null) |
| 522 | + { |
| 523 | + var aggregateExpression = new DocumentsAggregateExpressionDefinition<TDocument>(documents, documentSerializer); |
| 524 | + return Documents(aggregateExpression, documentSerializer); |
| 525 | + } |
| 526 | + |
480 | 527 | /// <summary>
|
481 | 528 | /// Creates a $facet stage.
|
482 | 529 | /// </summary>
|
|
0 commit comments