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 collection.distinct() in Java shell #1073

Merged
merged 2 commits into from
Aug 23, 2021
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 @@ -385,8 +385,16 @@ internal class JavaServiceProvider(private val client: MongoClient,
}

@HostAccess.Export
override fun distinct(database: String, collection: String, fieldName: String, filter: Value?, options: Value?): Value = promise<Any?> {
Left(NotImplementedError())
override fun distinct(database: String, collection: String, fieldName: String, filter: Value?, options: Value?): Value = promise {
val filter = toDocument(filter, "filter") ?: Document()
val options = toDocument(options, "options") ?: Document()
getDatabase(database, null).flatMap { db ->
convert(null, distinctConverters, distinctDefaultConverter, options).map { _ ->
val fieldClass = db.getCollection(collection).find(filter).asSequence().take(1_000_000).firstNotNullOfOrNull { it[fieldName] }?.javaClass
?: throw Exception("Cannot determine type of field $fieldName")
converter.toJs(db.getCollection(collection).distinct(fieldName, filter, fieldClass))
}
}
}

@HostAccess.Export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,10 @@ internal val insertManyConverters: Map<String, (InsertManyOptions, Any?) -> Eith

internal val insertManyDefaultConverter = unrecognizedField<InsertManyOptions>("insert many options")

internal val distinctConverters: Map<String, (Any?, Any?) -> Either<Any?>> = mapOf()

internal val distinctDefaultConverter = unrecognizedField<Any?>("distinct options")

internal val renameCollectionConverters: Map<String, (RenameCollectionOptions, Any?) -> Either<RenameCollectionOptions>> = mapOf(
typed("dropTarget", Boolean::class.java) { opt, value ->
opt.dropTarget(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
kotlin.NotImplementedError: An operation is not implemented.
[ 1, 2, 3 ]
[ "hello", "world" ]
org.bson.BsonInvalidOperationException: Invalid numeric type, found: STRING
[ 1 ]
[ { }, { "a": 1 } ]
[ "hello" ]
com.mongodb.mongosh.result.CommandException: unrecognized distinct options field: collation
20 changes: 16 additions & 4 deletions packages/java-shell/src/test/resources/collection/distinct.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// before
db.coll.deleteMany({});
db.coll.insertOne({a: 1});
db.coll.insertOne({a: 2});
db.coll.insertOne({a: 3});
db.coll.insertOne({num: 1, str: "hello", mixedType: 1, doc: {}, category: "café"});
db.coll.insertOne({num: 2, str: "world", mixedType: "hello", doc: {}, category: "cafe"});
db.coll.insertOne({num: 3, str: "hello", mixedType: 2, doc: {a: 1}, category: "cafE", onlyInOneDoc: 1});
// command
db.coll.distinct("a");
db.coll.distinct("num");
// command
db.coll.distinct("str");
// command
db.coll.distinct("mixedType");
// command
db.coll.distinct("onlyInOneDoc");
// command
db.coll.distinct("doc");
// command
db.coll.distinct("str", {num: 1});
// command
db.coll.distinct("category", {}, {collation: {locale: "fr", strength: 1}});
// clear
db.coll.drop();