Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support counting documents with options #107

Merged
merged 1 commit into from
Jun 24, 2024
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
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