[CSHARP-335] MongoCollection.Count() method does not respect SlaveOk Created: 05/Oct/11  Updated: 02/Apr/15  Resolved: 06/Oct/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.2
Fix Version/s: 1.3

Type: Bug Priority: Major - P3
Reporter: Alan L. Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: replication
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-336 Add support for sending commands to s... Closed

 Description   

mongoDB Setup
Replica Set with all secondaries, no primary is up.

Sample Code:

var server = MongoServer.Create(ConfigurationManager.AppSettings["connectionString"]);
var db = server.GetDatabase("myDb", SafeMode.False);

var settings = db.CreateCollectionSettings<Task>("Tasks");
settings.SlaveOk = true;
settings.SafeMode = SafeMode.False;

var taskCollection = db.GetCollection(settings);

var tasks = taskCollection.FindAll();

var taskCount = taskCollection.Count();

I can get the collection of items fine from taskCollection.FindAll(), but if I call taskCollection.Count(), driver throws the following error:

MongoDB.Driver.MongoConnectionException: Unable to connect to the primary member of the replica set: No connection could be made because the target machine actively refused it

Some troubleshooting leads me to believe the MongoCollection.Count() implementation is not respecting the SlaveOk setting.



 Comments   
Comment by Robert Stam [ 06/Oct/11 ]

See CSHARP-336 instead.

Comment by Robert Stam [ 06/Oct/11 ]

I'm opening a new issue (CSHARP-336) which addresses commands in general, not just the Count command.

Comment by Robert Stam [ 06/Oct/11 ]

You are correct. Count is actually executed as a command, and all commands are sent to the primary.

Many commands only work on the primary. While count theoretically could be sent to a secondary, the driver doesn't know (at least not yet) on a command by command case whether that particular command could be sent to the secondary.

You could work around this by working at a lower level and sending the equivalent query to the $cmd collection using FindOne, in which case slaveOk would be honored. Let me know if you are interested in doing that and need help writing it that way.

Generated at Wed Feb 07 21:36:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.