@@ -35,20 +35,21 @@ namespace MongoDB.Driver
35
35
public class MongoCursorEnumerator < TDocument > : IEnumerator < TDocument >
36
36
{
37
37
// private fields
38
+ private readonly MongoCursor < TDocument > _cursor ;
39
+ private readonly QueryFlags _queryFlags ;
40
+ private readonly ReadPreference _readPreference ;
41
+
38
42
private bool _disposed = false ;
39
43
private bool _started = false ;
40
44
private bool _done = false ;
41
- private MongoCursor < TDocument > _cursor ;
42
45
private MongoServerInstance _serverInstance ; // set when first request is sent to server instance
43
46
private int _count ;
44
47
private int _positiveLimit ;
45
48
private MongoReplyMessage < TDocument > _reply ;
46
49
private int _replyIndex ;
47
50
private ResponseFlags _responseFlags ;
48
51
private long _openCursorId ;
49
- private ReadPreference _readPreference ;
50
- private QueryFlags _queryFlags ;
51
-
52
+
52
53
// constructors
53
54
/// <summary>
54
55
/// Initializes a new instance of the MongoCursorEnumerator class.
@@ -58,6 +59,22 @@ public MongoCursorEnumerator(MongoCursor<TDocument> cursor)
58
59
{
59
60
_cursor = cursor ;
60
61
_positiveLimit = cursor . Limit >= 0 ? cursor . Limit : - cursor . Limit ;
62
+ _readPreference = _cursor . ReadPreference ;
63
+ _queryFlags = _cursor . Flags ;
64
+
65
+ if ( _readPreference . ReadPreferenceMode != ReadPreferenceMode . Primary && _cursor . Collection . Name == "$cmd" )
66
+ {
67
+ var queryDocument = _cursor . Query . ToBsonDocument ( ) ;
68
+ var isSecondaryOk = MongoDefaults . CanCommandBeSentToSecondary ( queryDocument ) ;
69
+ if ( ! isSecondaryOk )
70
+ {
71
+ // if the command can't be sent to a secondary, then we use primary here
72
+ // regardless of the user's choice.
73
+ _readPreference = ReadPreference . Primary ;
74
+ // remove the slaveOk bit from the flags
75
+ _queryFlags &= ~ QueryFlags . SlaveOk ;
76
+ }
77
+ }
61
78
}
62
79
63
80
// public properties
@@ -208,22 +225,6 @@ private MongoConnection AcquireConnection()
208
225
{
209
226
if ( _serverInstance == null )
210
227
{
211
- _readPreference = _cursor . ReadPreference ;
212
- _queryFlags = _cursor . Flags ;
213
- if ( _readPreference . ReadPreferenceMode != ReadPreferenceMode . Primary && _cursor . Collection . Name == "$cmd" )
214
- {
215
- var queryDocument = _cursor . Query . ToBsonDocument ( ) ;
216
- var isSecondaryOk = MongoDefaults . CanCommandBeSentToSecondary ( queryDocument ) ;
217
- if ( ! isSecondaryOk )
218
- {
219
- // if the command can't be sent to a secondary, then we use primary here
220
- // regardless of the user's choice.
221
- _readPreference = ReadPreference . Primary ;
222
- // remove the slaveOk bit from the flags
223
- _queryFlags &= ~ QueryFlags . SlaveOk ;
224
- }
225
- }
226
-
227
228
// first time we need a connection let Server.AcquireConnection pick the server instance
228
229
var connection = _cursor . Server . AcquireConnection ( _cursor . Database , _readPreference ) ;
229
230
_serverInstance = connection . ServerInstance ;
@@ -349,8 +350,8 @@ private void KillCursor()
349
350
private IMongoQuery WrapQuery ( )
350
351
{
351
352
BsonDocument formattedReadPreference = null ;
352
- if ( _serverInstance . InstanceType == MongoServerInstanceType . ShardRouter
353
- && _readPreference . ReadPreferenceMode != ReadPreferenceMode . Primary )
353
+ if ( _serverInstance . InstanceType == MongoServerInstanceType . ShardRouter &&
354
+ _readPreference . ReadPreferenceMode != ReadPreferenceMode . Primary )
354
355
{
355
356
BsonArray tagSetsArray = null ;
356
357
if ( _readPreference . TagSets != null )
0 commit comments