@@ -38,6 +38,7 @@ public abstract class MongoCollection
38
38
// private fields
39
39
private MongoServer _server ;
40
40
private MongoDatabase _database ;
41
+ private MongoDatabase _commandDatabase ; // used to run commands with this collection's settings
41
42
private MongoCollectionSettings _settings ;
42
43
private string _name ;
43
44
@@ -67,6 +68,21 @@ protected MongoCollection(MongoDatabase database, MongoCollectionSettings settin
67
68
_database = database ;
68
69
_settings = settings . FrozenCopy ( ) ;
69
70
_name = settings . CollectionName ;
71
+
72
+ // note: if the settings are compatible _commandDatabase will end up being the same instance as _database
73
+ // need to check for $cmd to avoid infinite recursion
74
+ if ( _name != "$cmd" )
75
+ {
76
+ var commandDatabaseSettings = _database . Settings . Clone ( ) ;
77
+ commandDatabaseSettings . GuidRepresentation = _settings . GuidRepresentation ;
78
+ commandDatabaseSettings . ReadPreference = _settings . ReadPreference ;
79
+ commandDatabaseSettings . SafeMode = _settings . SafeMode ; // not really relevant to commands since they are all queries anyway
80
+ _commandDatabase = _server . GetDatabase ( commandDatabaseSettings ) ;
81
+ }
82
+ else
83
+ {
84
+ _commandDatabase = _database ;
85
+ }
70
86
}
71
87
72
88
// public properties
@@ -121,7 +137,7 @@ public virtual AggregateResult Aggregate(IEnumerable<BsonDocument> operations)
121
137
{ "aggregate" , _name } ,
122
138
{ "pipeline" , pipeline }
123
139
} ;
124
- return _database . RunCommandAs < AggregateResult > ( aggregateCommand ) ;
140
+ return _commandDatabase . RunCommandAs < AggregateResult > ( aggregateCommand ) ;
125
141
}
126
142
127
143
/// <summary>
@@ -155,7 +171,7 @@ public virtual long Count(IMongoQuery query)
155
171
{ "count" , _name } ,
156
172
{ "query" , BsonDocumentWrapper . Create ( query ) , query != null } // query is optional
157
173
} ;
158
- var result = _database . RunCommand ( command ) ;
174
+ var result = _commandDatabase . RunCommand ( command ) ;
159
175
return result . Response [ "n" ] . ToInt64 ( ) ;
160
176
}
161
177
@@ -231,7 +247,7 @@ public virtual IEnumerable<BsonValue> Distinct(string key, IMongoQuery query)
231
247
{ "key" , key } ,
232
248
{ "query" , BsonDocumentWrapper . Create ( query ) , query != null } // query is optional
233
249
} ;
234
- var result = _database . RunCommand ( command ) ;
250
+ var result = _commandDatabase . RunCommand ( command ) ;
235
251
return result . Response [ "values" ] . AsBsonArray ;
236
252
}
237
253
@@ -298,7 +314,7 @@ public virtual CommandResult DropIndexByName(string indexName)
298
314
} ;
299
315
try
300
316
{
301
- return _database . RunCommand ( command ) ;
317
+ return _commandDatabase . RunCommand ( command ) ;
302
318
}
303
319
catch ( MongoCommandException ex )
304
320
{
@@ -457,7 +473,7 @@ public virtual FindAndModifyResult FindAndModify(
457
473
} ;
458
474
try
459
475
{
460
- return _database . RunCommandAs < FindAndModifyResult > ( command ) ;
476
+ return _commandDatabase . RunCommandAs < FindAndModifyResult > ( command ) ;
461
477
}
462
478
catch ( MongoCommandException ex )
463
479
{
@@ -494,7 +510,7 @@ public virtual FindAndModifyResult FindAndRemove(IMongoQuery query, IMongoSortBy
494
510
} ;
495
511
try
496
512
{
497
- return _database . RunCommandAs < FindAndModifyResult > ( command ) ;
513
+ return _commandDatabase . RunCommandAs < FindAndModifyResult > ( command ) ;
498
514
}
499
515
catch ( MongoCommandException ex )
500
516
{
@@ -638,7 +654,7 @@ public virtual GeoHaystackSearchResult GeoHaystackSearchAs(
638
654
command . Merge ( options . ToBsonDocument ( ) ) ;
639
655
var geoHaystackSearchResultDefinition = typeof ( GeoHaystackSearchResult < > ) ;
640
656
var geoHaystackSearchResultType = geoHaystackSearchResultDefinition . MakeGenericType ( documentType ) ;
641
- return ( GeoHaystackSearchResult ) _database . RunCommandAs ( geoHaystackSearchResultType , command ) ;
657
+ return ( GeoHaystackSearchResult ) _commandDatabase . RunCommandAs ( geoHaystackSearchResultType , command ) ;
642
658
}
643
659
644
660
/// <summary>
@@ -684,7 +700,7 @@ public virtual GeoNearResult<TDocument> GeoNearAs<TDocument>(
684
700
{ "query" , BsonDocumentWrapper . Create ( query ) , query != null } // query is optional
685
701
} ;
686
702
command . Merge ( options . ToBsonDocument ( ) ) ;
687
- return _database . RunCommandAs < GeoNearResult < TDocument > > ( command ) ;
703
+ return _commandDatabase . RunCommandAs < GeoNearResult < TDocument > > ( command ) ;
688
704
}
689
705
690
706
/// <summary>
@@ -729,7 +745,7 @@ public virtual GeoNearResult GeoNearAs(
729
745
command . Merge ( options . ToBsonDocument ( ) ) ;
730
746
var geoNearResultDefinition = typeof ( GeoNearResult < > ) ;
731
747
var geoNearResultType = geoNearResultDefinition . MakeGenericType ( documentType ) ;
732
- return ( GeoNearResult ) _database . RunCommandAs ( geoNearResultType , command ) ;
748
+ return ( GeoNearResult ) _commandDatabase . RunCommandAs ( geoNearResultType , command ) ;
733
749
}
734
750
735
751
/// <summary>
@@ -750,7 +766,7 @@ public virtual GetIndexesResult GetIndexes()
750
766
public virtual CollectionStatsResult GetStats ( )
751
767
{
752
768
var command = new CommandDocument ( "collstats" , _name ) ;
753
- return _database . RunCommandAs < CollectionStatsResult > ( command ) ;
769
+ return _commandDatabase . RunCommandAs < CollectionStatsResult > ( command ) ;
754
770
}
755
771
756
772
/// <summary>
@@ -828,7 +844,7 @@ public virtual IEnumerable<BsonDocument> Group(
828
844
}
829
845
}
830
846
} ;
831
- var result = _database . RunCommand ( command ) ;
847
+ var result = _commandDatabase . RunCommand ( command ) ;
832
848
return result . Response [ "retval" ] . AsBsonArray . Values . Cast < BsonDocument > ( ) ;
833
849
}
834
850
@@ -875,7 +891,7 @@ public virtual IEnumerable<BsonDocument> Group(
875
891
}
876
892
}
877
893
} ;
878
- var result = _database . RunCommand ( command ) ;
894
+ var result = _commandDatabase . RunCommand ( command ) ;
879
895
return result . Response [ "retval" ] . AsBsonArray . Values . Cast < BsonDocument > ( ) ;
880
896
}
881
897
@@ -1203,7 +1219,7 @@ public virtual MapReduceResult MapReduce(
1203
1219
{ "reduce" , reduce }
1204
1220
} ;
1205
1221
command . Add ( options . ToBsonDocument ( ) ) ;
1206
- var result = _database . RunCommandAs < MapReduceResult > ( command ) ;
1222
+ var result = _commandDatabase . RunCommandAs < MapReduceResult > ( command ) ;
1207
1223
result . SetInputDatabase ( _database ) ;
1208
1224
return result ;
1209
1225
}
@@ -1259,7 +1275,7 @@ public virtual MapReduceResult MapReduce(BsonJavaScript map, BsonJavaScript redu
1259
1275
public virtual CommandResult ReIndex ( )
1260
1276
{
1261
1277
var command = new CommandDocument ( "reIndex" , _name ) ;
1262
- return _database . RunCommand ( command ) ;
1278
+ return _commandDatabase . RunCommand ( command ) ;
1263
1279
}
1264
1280
1265
1281
/// <summary>
@@ -1604,7 +1620,7 @@ public virtual SafeModeResult Update(
1604
1620
public virtual ValidateCollectionResult Validate ( )
1605
1621
{
1606
1622
var command = new CommandDocument ( "validate" , _name ) ;
1607
- return _database . RunCommandAs < ValidateCollectionResult > ( command ) ;
1623
+ return _commandDatabase . RunCommandAs < ValidateCollectionResult > ( command ) ;
1608
1624
}
1609
1625
1610
1626
// internal methods
0 commit comments