Skip to content

Commit

Permalink
Support counting documents with options
Browse files Browse the repository at this point in the history
  • Loading branch information
dieproht committed Jun 24, 2024
1 parent 9dd0149 commit 475b55c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions molly-core/src/main/scala/molly/core/MollyCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import molly.core.reactivestreams.fromSinglePublisher
import molly.core.reactivestreams.fromStreamPublisher
import molly.core.reactivestreams.fromVoidPublisher
import molly.core.syntax.model.BulkWriteOptions
import molly.core.syntax.model.CountOptions
import molly.core.syntax.model.CreateIndexOptions
import molly.core.syntax.model.FindOneAndReplaceOptions
import molly.core.syntax.model.FindOneAndUpdateOptions
Expand Down Expand Up @@ -64,6 +65,11 @@ final case class MollyCollection[F[_], A] private[core] (private[core] val deleg
*/
def countDocuments(filter: Bson): F[Long] = fromSinglePublisher(delegate.countDocuments(filter)).map(_.toLong)

/** [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#countDocuments(org.bson.conversions.Bson,com.mongodb.client.model.CountOptions)]]
*/
def countDocuments(filter: Bson, options: CountOptions): F[Long] =
fromSinglePublisher(delegate.countDocuments(filter, options)).map(_.toLong)

/** [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#createIndex(org.bson.conversions.Bson)]]
*/
def createIndex(key: Bson): F[String] = fromSinglePublisher(delegate.createIndex(key))
Expand Down
5 changes: 5 additions & 0 deletions molly-core/src/main/scala/molly/core/syntax/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ trait model:
object BulkWriteOptions:
def apply(): BulkWriteOptions = new BulkWriteOptions()

type CountOptions = com.mongodb.client.model.CountOptions

object CountOptions:
def apply(): CountOptions = new CountOptions()

type CreateIndexOptions = com.mongodb.client.model.CreateIndexOptions

object CreateIndexOptions:
Expand Down
13 changes: 13 additions & 0 deletions molly-core/src/test/scala/molly/core/MollyCollectionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mongodb.client.model.ReturnDocument
import com.mongodb.client.model.Sorts
import com.mongodb.client.model.Updates
import molly.core.syntax.bsondocument.BsonDocumentCollection
import molly.core.syntax.model.CountOptions
import molly.core.syntax.model.CreateIndexOptions
import molly.core.syntax.model.DeleteOneModel
import molly.core.syntax.model.FindOneAndReplaceOptions
Expand Down Expand Up @@ -112,6 +113,18 @@ object MollyCollectionTest extends IOSuite with TestContainerForAll[IO] with Mol
count <- coll.countDocuments(Filters.regex("foo", "bar.*"))
yield expect(count == 2L)

test("countDocuments: count documents given a filter and options"): containers =>
withClient(containers): (client: MollyClient[IO]) =>
val doc1 = new BsonDocument("foo", new BsonString("bar"))
val doc2 = new BsonDocument("foo", new BsonString("baz"))
val doc3 = new BsonDocument("foo", new BsonString("barry"))
for
db <- client.getDatabase("test")
coll <- db.getCollection("countDocuments2")
_ <- coll.insertMany(Seq(doc1, doc2, doc3))
count <- coll.countDocuments(Filters.regex("foo", "bar.*"), CountOptions().limit(1))
yield expect(count == 1L)

test("create and list index"): containers =>
withClient(containers): (client: MollyClient[IO]) =>
for
Expand Down

0 comments on commit 475b55c

Please sign in to comment.