Skip to content

Commit a6eafbd

Browse files
author
rstam
committed
CSHARP-580: command based helper methods in MongoCollection should use the collection's ReadPreference.
1 parent 34bbb5b commit a6eafbd

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

Diff for: Driver/Core/MongoCollection.cs

+31-15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class MongoCollection
3838
// private fields
3939
private MongoServer _server;
4040
private MongoDatabase _database;
41+
private MongoDatabase _commandDatabase; // used to run commands with this collection's settings
4142
private MongoCollectionSettings _settings;
4243
private string _name;
4344

@@ -67,6 +68,21 @@ protected MongoCollection(MongoDatabase database, MongoCollectionSettings settin
6768
_database = database;
6869
_settings = settings.FrozenCopy();
6970
_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+
}
7086
}
7187

7288
// public properties
@@ -121,7 +137,7 @@ public virtual AggregateResult Aggregate(IEnumerable<BsonDocument> operations)
121137
{ "aggregate", _name },
122138
{ "pipeline", pipeline }
123139
};
124-
return _database.RunCommandAs<AggregateResult>(aggregateCommand);
140+
return _commandDatabase.RunCommandAs<AggregateResult>(aggregateCommand);
125141
}
126142

127143
/// <summary>
@@ -155,7 +171,7 @@ public virtual long Count(IMongoQuery query)
155171
{ "count", _name },
156172
{ "query", BsonDocumentWrapper.Create(query), query != null } // query is optional
157173
};
158-
var result = _database.RunCommand(command);
174+
var result = _commandDatabase.RunCommand(command);
159175
return result.Response["n"].ToInt64();
160176
}
161177

@@ -231,7 +247,7 @@ public virtual IEnumerable<BsonValue> Distinct(string key, IMongoQuery query)
231247
{ "key", key },
232248
{ "query", BsonDocumentWrapper.Create(query), query != null } // query is optional
233249
};
234-
var result = _database.RunCommand(command);
250+
var result = _commandDatabase.RunCommand(command);
235251
return result.Response["values"].AsBsonArray;
236252
}
237253

@@ -298,7 +314,7 @@ public virtual CommandResult DropIndexByName(string indexName)
298314
};
299315
try
300316
{
301-
return _database.RunCommand(command);
317+
return _commandDatabase.RunCommand(command);
302318
}
303319
catch (MongoCommandException ex)
304320
{
@@ -457,7 +473,7 @@ public virtual FindAndModifyResult FindAndModify(
457473
};
458474
try
459475
{
460-
return _database.RunCommandAs<FindAndModifyResult>(command);
476+
return _commandDatabase.RunCommandAs<FindAndModifyResult>(command);
461477
}
462478
catch (MongoCommandException ex)
463479
{
@@ -494,7 +510,7 @@ public virtual FindAndModifyResult FindAndRemove(IMongoQuery query, IMongoSortBy
494510
};
495511
try
496512
{
497-
return _database.RunCommandAs<FindAndModifyResult>(command);
513+
return _commandDatabase.RunCommandAs<FindAndModifyResult>(command);
498514
}
499515
catch (MongoCommandException ex)
500516
{
@@ -638,7 +654,7 @@ public virtual GeoHaystackSearchResult GeoHaystackSearchAs(
638654
command.Merge(options.ToBsonDocument());
639655
var geoHaystackSearchResultDefinition = typeof(GeoHaystackSearchResult<>);
640656
var geoHaystackSearchResultType = geoHaystackSearchResultDefinition.MakeGenericType(documentType);
641-
return (GeoHaystackSearchResult)_database.RunCommandAs(geoHaystackSearchResultType, command);
657+
return (GeoHaystackSearchResult)_commandDatabase.RunCommandAs(geoHaystackSearchResultType, command);
642658
}
643659

644660
/// <summary>
@@ -684,7 +700,7 @@ public virtual GeoNearResult<TDocument> GeoNearAs<TDocument>(
684700
{ "query", BsonDocumentWrapper.Create(query), query != null } // query is optional
685701
};
686702
command.Merge(options.ToBsonDocument());
687-
return _database.RunCommandAs<GeoNearResult<TDocument>>(command);
703+
return _commandDatabase.RunCommandAs<GeoNearResult<TDocument>>(command);
688704
}
689705

690706
/// <summary>
@@ -729,7 +745,7 @@ public virtual GeoNearResult GeoNearAs(
729745
command.Merge(options.ToBsonDocument());
730746
var geoNearResultDefinition = typeof(GeoNearResult<>);
731747
var geoNearResultType = geoNearResultDefinition.MakeGenericType(documentType);
732-
return (GeoNearResult)_database.RunCommandAs(geoNearResultType, command);
748+
return (GeoNearResult)_commandDatabase.RunCommandAs(geoNearResultType, command);
733749
}
734750

735751
/// <summary>
@@ -750,7 +766,7 @@ public virtual GetIndexesResult GetIndexes()
750766
public virtual CollectionStatsResult GetStats()
751767
{
752768
var command = new CommandDocument("collstats", _name);
753-
return _database.RunCommandAs<CollectionStatsResult>(command);
769+
return _commandDatabase.RunCommandAs<CollectionStatsResult>(command);
754770
}
755771

756772
/// <summary>
@@ -828,7 +844,7 @@ public virtual IEnumerable<BsonDocument> Group(
828844
}
829845
}
830846
};
831-
var result = _database.RunCommand(command);
847+
var result = _commandDatabase.RunCommand(command);
832848
return result.Response["retval"].AsBsonArray.Values.Cast<BsonDocument>();
833849
}
834850

@@ -875,7 +891,7 @@ public virtual IEnumerable<BsonDocument> Group(
875891
}
876892
}
877893
};
878-
var result = _database.RunCommand(command);
894+
var result = _commandDatabase.RunCommand(command);
879895
return result.Response["retval"].AsBsonArray.Values.Cast<BsonDocument>();
880896
}
881897

@@ -1203,7 +1219,7 @@ public virtual MapReduceResult MapReduce(
12031219
{ "reduce", reduce }
12041220
};
12051221
command.Add(options.ToBsonDocument());
1206-
var result = _database.RunCommandAs<MapReduceResult>(command);
1222+
var result = _commandDatabase.RunCommandAs<MapReduceResult>(command);
12071223
result.SetInputDatabase(_database);
12081224
return result;
12091225
}
@@ -1259,7 +1275,7 @@ public virtual MapReduceResult MapReduce(BsonJavaScript map, BsonJavaScript redu
12591275
public virtual CommandResult ReIndex()
12601276
{
12611277
var command = new CommandDocument("reIndex", _name);
1262-
return _database.RunCommand(command);
1278+
return _commandDatabase.RunCommand(command);
12631279
}
12641280

12651281
/// <summary>
@@ -1604,7 +1620,7 @@ public virtual SafeModeResult Update(
16041620
public virtual ValidateCollectionResult Validate()
16051621
{
16061622
var command = new CommandDocument("validate", _name);
1607-
return _database.RunCommandAs<ValidateCollectionResult>(command);
1623+
return _commandDatabase.RunCommandAs<ValidateCollectionResult>(command);
16081624
}
16091625

16101626
// internal methods

0 commit comments

Comments
 (0)