Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mongodb.client.AggregateIterable
import com.mongodb.client.FindIterable
import com.mongodb.client.MongoDatabase
import com.mongodb.client.MongoIterable
import com.mongodb.client.model.CountOptions
import com.mongodb.mongosh.MongoShellConverter
import org.bson.Document
import org.graalvm.polyglot.Value
Expand Down Expand Up @@ -39,7 +40,7 @@ internal abstract class BaseMongoIterableHelper<T : MongoIterable<*>>(val iterab
open fun hint(v: Document): Unit = throw NotImplementedError("hint is not supported")
open fun collation(v: Document): Unit = throw NotImplementedError("collation is not supported")
open fun allowPartialResults(): Unit = throw NotImplementedError("allowPartialResults is not supported")
open fun count(): Int = throw NotImplementedError("count is not supported")
open fun count(): Long = throw NotImplementedError("count is not supported")
open fun maxTimeMS(v: Long): Unit = throw NotImplementedError("maxTimeMS is not supported")
open fun noCursorTimeout(): Unit = throw NotImplementedError("noCursorTimeout is not supported")
open fun oplogReplay(): Unit = throw NotImplementedError("oplogReplay is not supported")
Expand Down Expand Up @@ -157,6 +158,14 @@ internal class FindIterableHelper(iterable: FindIterable<out Any?>,
override fun sort(spec: Document) = set("sort", spec)
override fun tailable() = set("tailable", CursorType.Tailable.toString())

override fun count(): Long {
check(createOptions != null) { "createOptions were not saved" }
val countOptionsMap = options.filterKeys { countOptionsConverters.containsKey(it) }
val countOptions = convert(CountOptions(), countOptionsConverters, countOptionsDefaultConverter, countOptionsMap).getOrThrow()
@Suppress("DEPRECATION")
return createOptions.db.getCollection(createOptions.collection).count(createOptions.find, countOptions)
}

override fun explain(verbosity: String?): Any? {
set("explain", true)
return iterable.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va
return this
}

/**
* cursor.objsLeftInBatch()
*/
override fun bufferedCount(): Int {
throw NotImplementedError("bufferedCount is not supported")
}
Expand Down Expand Up @@ -89,7 +92,7 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va
}

@HostAccess.Export
override fun count(): Int {
override fun count(): Long {
checkQueryNotExecuted()
return helper.count()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ServiceProviderCursor {
fun isClosed(): Boolean
fun collation(v: Value): ServiceProviderCursor
fun comment(v: String): ServiceProviderCursor
fun count(): Int
fun count(): Long
fun forEach(func: Value)
fun hasNext(): Boolean
fun hint(v: Value): ServiceProviderCursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CollectionTest : ShellTestCase() {
@Test fun testEstimatedDocumentCount() = test()
@Test fun testEstimatedDocumentCountWithMaxTime() = test()
@Test fun testFind() = test()
@Test fun testFindAndCount() = test()
@Test fun testFindMap() = test()
@Test fun testFindOne() = test()
@Test fun testFindOneAndDelete() = test()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
1
2
11 changes: 11 additions & 0 deletions packages/java-shell/src/test/resources/collection/findAndCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// before
db.coll.deleteMany({});
db.coll.insertMany([{v: 1}, {v: 2}]);
// command
db.coll.find({v: 1}).count();
// command
db.coll.find().limit(1).count();
// command
db.coll.find({}, {}, {"comment": "hello"}).count();
// clear
db.coll.drop();