Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Array operations #32

Merged
merged 9 commits into from
Aug 16, 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
17 changes: 17 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/Docker_Compose.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

services:
mongodb:
image: mongo:7
environment:
- MONGODB_INITDB_ROOT_USERNAME=user
- MONGODB_INITDB_ROOT_PASSWORD=password
ports:
- "27017:27017"
23 changes: 18 additions & 5 deletions driver-sync/src/main/kotlin/NativeMongoCollection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.mongodb.kotlin.client.FindIterable
import fr.qsh.ktmongo.dsl.LowLevelApi
import fr.qsh.ktmongo.dsl.expr.FilterExpression
import fr.qsh.ktmongo.dsl.expr.UpdateExpression
import fr.qsh.ktmongo.dsl.expr.common.CompoundExpression
import fr.qsh.ktmongo.dsl.expr.common.AbstractCompoundExpression
import org.bson.BsonDocument
import org.bson.BsonDocumentWriter
import com.mongodb.kotlin.client.MongoCollection as OfficialMongoCollection
Expand All @@ -22,7 +22,7 @@ class NativeMongoCollection<Document : Any>(
fun asOfficialMongoCollection() = unsafe

@OptIn(LowLevelApi::class)
private fun CompoundExpression.toBsonDocument(): BsonDocument {
private fun AbstractCompoundExpression.toBsonDocument(): BsonDocument {
val bson = BsonDocument()

BsonDocumentWriter(bson).use { writer ->
Expand All @@ -32,6 +32,19 @@ class NativeMongoCollection<Document : Any>(
return bson
}

@OptIn(LowLevelApi::class)
private fun AbstractCompoundExpression.toNestedBsonDocument(): BsonDocument {
val bson = BsonDocument()

BsonDocumentWriter(bson).use { writer ->
writer.writeStartDocument()
this.writeTo(writer)
writer.writeEndDocument()
}

return bson
}

// region Find

override fun find(): FindIterable<Document> =
Expand Down Expand Up @@ -94,7 +107,7 @@ class NativeMongoCollection<Document : Any>(

val updateBson = UpdateExpression<Document>(unsafe.codecRegistry)
.apply(update)
.toBsonDocument()
.toNestedBsonDocument()

return when (val session = getCurrentSession()) {
null -> unsafe.updateOne(filterBson, updateBson, options)
Expand All @@ -113,7 +126,7 @@ class NativeMongoCollection<Document : Any>(

val updateBson = UpdateExpression<Document>(unsafe.codecRegistry)
.apply(update)
.toBsonDocument()
.toNestedBsonDocument()

return when (val session = getCurrentSession()) {
null -> unsafe.updateMany(filterBson, updateBson, options)
Expand All @@ -132,7 +145,7 @@ class NativeMongoCollection<Document : Any>(

val updateBson = UpdateExpression<Document>(unsafe.codecRegistry)
.apply(update)
.toBsonDocument()
.toNestedBsonDocument()

return when (val session = getCurrentSession()) {
null -> unsafe.findOneAndUpdate(filterBson, updateBson, options)
Expand Down
2 changes: 2 additions & 0 deletions dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

### Filter

- [`$all`][fr.qsh.ktmongo.dsl.expr.FilterExpression.containsAll]
- [`$and`][fr.qsh.ktmongo.dsl.expr.FilterExpression.and]
- [`$elemMatch`][fr.qsh.ktmongo.dsl.expr.FilterExpression.anyObject]
- [`$eq`][fr.qsh.ktmongo.dsl.expr.FilterExpression.eq]
- [`$exists`][fr.qsh.ktmongo.dsl.expr.FilterExpression.exists]
- [`$gt`][fr.qsh.ktmongo.dsl.expr.FilterExpression.gt]
Expand Down
8 changes: 7 additions & 1 deletion dsl/src/main/kotlin/DocumentWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ internal inline fun BsonWriter.writeArray(name: String, block: () -> Unit) {
internal fun <T> BsonWriter.writeObject(value: T, codec: CodecRegistry) {
@Suppress("UNCHECKED_CAST") // Kotlin doesn't smart-cast here, but should, this is safe
(codec.get(value!!::class.java) as Encoder<T>)
.encode(this, value, EncoderContext.builder().build())
.encode(
this,
value,
EncoderContext.builder()
.isEncodingCollectibleDocument(true)
.build()
)
}
Loading
Loading