Skip to content

Commit eadb692

Browse files
authored
[java-shell] support db.collection.find().count() (#450)
1 parent 8ce2fd0 commit eadb692

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/BaseMongoIterableHelper.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.mongodb.client.AggregateIterable
77
import com.mongodb.client.FindIterable
88
import com.mongodb.client.MongoDatabase
99
import com.mongodb.client.MongoIterable
10+
import com.mongodb.client.model.CountOptions
1011
import com.mongodb.mongosh.MongoShellConverter
1112
import org.bson.Document
1213
import org.graalvm.polyglot.Value
@@ -39,7 +40,7 @@ internal abstract class BaseMongoIterableHelper<T : MongoIterable<*>>(val iterab
3940
open fun hint(v: Document): Unit = throw NotImplementedError("hint is not supported")
4041
open fun collation(v: Document): Unit = throw NotImplementedError("collation is not supported")
4142
open fun allowPartialResults(): Unit = throw NotImplementedError("allowPartialResults is not supported")
42-
open fun count(): Int = throw NotImplementedError("count is not supported")
43+
open fun count(): Long = throw NotImplementedError("count is not supported")
4344
open fun maxTimeMS(v: Long): Unit = throw NotImplementedError("maxTimeMS is not supported")
4445
open fun noCursorTimeout(): Unit = throw NotImplementedError("noCursorTimeout is not supported")
4546
open fun oplogReplay(): Unit = throw NotImplementedError("oplogReplay is not supported")
@@ -157,6 +158,14 @@ internal class FindIterableHelper(iterable: FindIterable<out Any?>,
157158
override fun sort(spec: Document) = set("sort", spec)
158159
override fun tailable() = set("tailable", CursorType.Tailable.toString())
159160

161+
override fun count(): Long {
162+
check(createOptions != null) { "createOptions were not saved" }
163+
val countOptionsMap = options.filterKeys { countOptionsConverters.containsKey(it) }
164+
val countOptions = convert(CountOptions(), countOptionsConverters, countOptionsDefaultConverter, countOptionsMap).getOrThrow()
165+
@Suppress("DEPRECATION")
166+
return createOptions.db.getCollection(createOptions.collection).count(createOptions.find, countOptions)
167+
}
168+
160169
override fun explain(verbosity: String?): Any? {
161170
set("explain", true)
162171
return iterable.first()

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/Cursor.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va
4949
return this
5050
}
5151

52+
/**
53+
* cursor.objsLeftInBatch()
54+
*/
5255
override fun bufferedCount(): Int {
5356
throw NotImplementedError("bufferedCount is not supported")
5457
}
@@ -89,7 +92,7 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va
8992
}
9093

9194
@HostAccess.Export
92-
override fun count(): Int {
95+
override fun count(): Long {
9396
checkQueryNotExecuted()
9497
return helper.count()
9598
}

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/ServiceProviderCursor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ServiceProviderCursor {
1313
fun isClosed(): Boolean
1414
fun collation(v: Value): ServiceProviderCursor
1515
fun comment(v: String): ServiceProviderCursor
16-
fun count(): Int
16+
fun count(): Long
1717
fun forEach(func: Value)
1818
fun hasNext(): Boolean
1919
fun hint(v: Value): ServiceProviderCursor

packages/java-shell/src/test/kotlin/com/mongodb/mongosh/CollectionTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CollectionTest : ShellTestCase() {
3232
@Test fun testEstimatedDocumentCount() = test()
3333
@Test fun testEstimatedDocumentCountWithMaxTime() = test()
3434
@Test fun testFind() = test()
35+
@Test fun testFindAndCount() = test()
3536
@Test fun testFindMap() = test()
3637
@Test fun testFindOne() = test()
3738
@Test fun testFindOneAndDelete() = test()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
1
3+
2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// before
2+
db.coll.deleteMany({});
3+
db.coll.insertMany([{v: 1}, {v: 2}]);
4+
// command
5+
db.coll.find({v: 1}).count();
6+
// command
7+
db.coll.find().limit(1).count();
8+
// command
9+
db.coll.find({}, {}, {"comment": "hello"}).count();
10+
// clear
11+
db.coll.drop();

0 commit comments

Comments
 (0)