@@ -43,6 +43,7 @@ public class AggregateOperation<TResult> : IReadOperation<IAsyncCursor<TResult>>
43
43
private Collation _collation ;
44
44
private readonly CollectionNamespace _collectionNamespace ;
45
45
private string _comment ;
46
+ private readonly DatabaseNamespace _databaseNamespace ;
46
47
private BsonValue _hint ;
47
48
private TimeSpan ? _maxAwaitTime ;
48
49
private TimeSpan ? _maxTime ;
@@ -53,6 +54,19 @@ public class AggregateOperation<TResult> : IReadOperation<IAsyncCursor<TResult>>
53
54
private bool ? _useCursor ;
54
55
55
56
// constructors
57
+ /// <summary>
58
+ /// Initializes a new instance of the <see cref="AggregateOperation{TResult}"/> class.
59
+ /// </summary>
60
+ /// <param name="databaseNamespace">The database namespace.</param>
61
+ /// <param name="pipeline">The pipeline.</param>
62
+ /// <param name="resultSerializer">The result value serializer.</param>
63
+ /// <param name="messageEncoderSettings">The message encoder settings.</param>
64
+ public AggregateOperation ( DatabaseNamespace databaseNamespace , IEnumerable < BsonDocument > pipeline , IBsonSerializer < TResult > resultSerializer , MessageEncoderSettings messageEncoderSettings )
65
+ : this ( pipeline , resultSerializer , messageEncoderSettings )
66
+ {
67
+ _databaseNamespace = Ensure . IsNotNull ( databaseNamespace , nameof ( databaseNamespace ) ) ;
68
+ }
69
+
56
70
/// <summary>
57
71
/// Initializes a new instance of the <see cref="AggregateOperation{TResult}"/> class.
58
72
/// </summary>
@@ -61,8 +75,13 @@ public class AggregateOperation<TResult> : IReadOperation<IAsyncCursor<TResult>>
61
75
/// <param name="resultSerializer">The result value serializer.</param>
62
76
/// <param name="messageEncoderSettings">The message encoder settings.</param>
63
77
public AggregateOperation ( CollectionNamespace collectionNamespace , IEnumerable < BsonDocument > pipeline , IBsonSerializer < TResult > resultSerializer , MessageEncoderSettings messageEncoderSettings )
78
+ : this ( pipeline , resultSerializer , messageEncoderSettings )
64
79
{
65
80
_collectionNamespace = Ensure . IsNotNull ( collectionNamespace , nameof ( collectionNamespace ) ) ;
81
+ }
82
+
83
+ private AggregateOperation ( IEnumerable < BsonDocument > pipeline , IBsonSerializer < TResult > resultSerializer , MessageEncoderSettings messageEncoderSettings )
84
+ {
66
85
_pipeline = Ensure . IsNotNull ( pipeline , nameof ( pipeline ) ) . ToList ( ) ;
67
86
_resultSerializer = Ensure . IsNotNull ( resultSerializer , nameof ( resultSerializer ) ) ;
68
87
_messageEncoderSettings = Ensure . IsNotNull ( messageEncoderSettings , nameof ( messageEncoderSettings ) ) ;
@@ -125,6 +144,17 @@ public string Comment
125
144
set { _comment = value ; }
126
145
}
127
146
147
+ /// <summary>
148
+ /// Gets the database namespace.
149
+ /// </summary>
150
+ /// <value>
151
+ /// The database namespace.
152
+ /// </value>
153
+ public DatabaseNamespace DatabaseNamespace
154
+ {
155
+ get { return _databaseNamespace ; }
156
+ }
157
+
128
158
/// <summary>
129
159
/// Gets or sets the hint. This must either be a BsonString representing the index name or a BsonDocument representing the key pattern of the index.
130
160
/// </summary>
@@ -276,7 +306,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
276
306
var readConcern = ReadConcernHelper . GetReadConcernForCommand ( session , connectionDescription , _readConcern ) ;
277
307
var command = new BsonDocument
278
308
{
279
- { "aggregate" , _collectionNamespace . CollectionName } ,
309
+ { "aggregate" , _collectionNamespace == null ? ( BsonValue ) 1 : _collectionNamespace . CollectionName } ,
280
310
{ "pipeline" , new BsonArray ( _pipeline ) } ,
281
311
{ "allowDiskUse" , ( ) => _allowDiskUse . Value , _allowDiskUse . HasValue } ,
282
312
{ "maxTimeMS" , ( ) => MaxTimeHelper . ToMaxTimeMS ( _maxTime . Value ) , _maxTime . HasValue } ,
@@ -325,7 +355,7 @@ private AsyncCursor<TResult> CreateCursorFromCursorResult(IChannelSourceHandle c
325
355
var getMoreChannelSource = new ServerChannelSource ( channelSource . Server , channelSource . Session . Fork ( ) ) ;
326
356
return new AsyncCursor < TResult > (
327
357
getMoreChannelSource ,
328
- CollectionNamespace ,
358
+ result . CollectionNamespace ,
329
359
command ,
330
360
result . Results ,
331
361
result . CursorId . GetValueOrDefault ( 0 ) ,
@@ -362,6 +392,7 @@ private void EnsureIsReadOnlyPipeline()
362
392
private class AggregateResult
363
393
{
364
394
public long ? CursorId ;
395
+ public CollectionNamespace CollectionNamespace ;
365
396
public TResult [ ] Results ;
366
397
}
367
398
@@ -420,18 +451,28 @@ public override AggregateResult Deserialize(BsonDeserializationContext context,
420
451
while ( reader . ReadBsonType ( ) != 0 )
421
452
{
422
453
var elementName = reader . ReadName ( ) ;
423
- if ( elementName == "id" )
424
- {
425
- result . CursorId = new Int64Serializer ( ) . Deserialize ( context ) ;
426
- }
427
- else if ( elementName == "firstBatch" )
454
+ switch ( elementName )
428
455
{
429
- var arraySerializer = new ArraySerializer < TResult > ( _resultSerializer ) ;
430
- result . Results = arraySerializer . Deserialize ( context ) ;
431
- }
432
- else
433
- {
434
- reader . SkipValue ( ) ;
456
+ case "id" :
457
+ result . CursorId = new Int64Serializer ( ) . Deserialize ( context ) ;
458
+ break ;
459
+
460
+ case "ns" :
461
+ var ns = reader . ReadString ( ) ;
462
+ var separatorIndex = ns . IndexOf ( '.' ) ;
463
+ var databaseName = ns . Substring ( 0 , separatorIndex ) ;
464
+ var collectionName = ns . Substring ( separatorIndex + 1 ) ;
465
+ result . CollectionNamespace = new CollectionNamespace ( new DatabaseNamespace ( databaseName ) , collectionName ) ;
466
+ break ;
467
+
468
+ case "firstBatch" :
469
+ var arraySerializer = new ArraySerializer < TResult > ( _resultSerializer ) ;
470
+ result . Results = arraySerializer . Deserialize ( context ) ;
471
+ break ;
472
+
473
+ default :
474
+ reader . SkipValue ( ) ;
475
+ break ;
435
476
}
436
477
}
437
478
reader . ReadEndDocument ( ) ;
0 commit comments