Skip to content

Commit 79cb423

Browse files
committed
Changed DBCollection.getStats to use the collection's default read preference, as per the server selection specification.
JAVA-1921
1 parent 5612f47 commit 79cb423

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

driver/src/main/com/mongodb/DBCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ public void dropIndexes(final String indexName) {
18551855
* @mongodb.driver.manual reference/command/collStats/ collStats Command
18561856
*/
18571857
public CommandResult getStats() {
1858-
return getDB().executeCommand(new BsonDocument("collStats", new BsonString(getName())));
1858+
return getDB().executeCommand(new BsonDocument("collStats", new BsonString(getName())), getReadPreference());
18591859
}
18601860

18611861
/**

driver/src/test/functional/com/mongodb/DBCollectionSpecification.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package com.mongodb
1818

1919
import com.mongodb.bulk.IndexRequest
20+
import com.mongodb.operation.CommandReadOperation
2021
import com.mongodb.operation.CreateIndexesOperation
2122
import org.bson.BsonDocument
2223
import org.bson.BsonInt32
2324
import org.bson.BsonString
25+
import org.bson.codecs.BsonDocumentCodec
2426
import spock.lang.Specification
2527

2628
import java.util.concurrent.TimeUnit
@@ -151,4 +153,20 @@ class DBCollectionSpecification extends Specification {
151153
then:
152154
thrown(IllegalArgumentException)
153155
}
156+
157+
def 'getStats should execute the expected command with the collection default read preference'() {
158+
given:
159+
def executor = new TestOperationExecutor([new BsonDocument('ok', new BsonInt32(1))]);
160+
def collection = new DB(getMongoClient(), 'myDatabase', executor).getCollection('test')
161+
collection.setReadPreference(ReadPreference.secondary())
162+
163+
when:
164+
collection.getStats()
165+
166+
then:
167+
expect executor.getReadOperation(), isTheSameAs(new CommandReadOperation('myDatabase',
168+
new BsonDocument('collStats', new BsonString('test')),
169+
new BsonDocumentCodec()))
170+
executor.getReadPreference() == collection.getReadPreference()
171+
}
154172
}

0 commit comments

Comments
 (0)