I was using KMongo and I’m now migrating to the new Kotlin Official driver.
I have a document with an array of data classes, like so:
@Serializable
data class ListDto(
@Contextual @SerialName("_id") val id: Id<ListDto> = ObjectId().toId(),
@Contextual var userId: Id<UserDto>,
var name: String,
val categories: MutableList<CategoryDto> = mutableListOf(),
var icon: String, // Single emoji at the moment
var color: String // Represented as #RRGGBB hex color
)
@Serializable
data class CategoryDto(
@Contextual @SerialName("_id") val id: Id<CategoryDto> = ObjectId().toId(),
var name: String,
var color: String // Represented as #010101 hex color
)
What I need to do is update a single property (for example the name) in one of the categories items, filtering via the id.
In Kmongo I was using
return col.findOneAndUpdate(
and(ListDto::id eq listId, ListDto::userId eq userId, (ListDto::categories / CategoryDto::id) eq categoryId),
set(*properties.toTypedArray()),
FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
)
And in the set update operations I was using the positional operator matching
Right now I can’t seem to find the replacement for (ListDto::categories / CategoryDto::id) eq categoryId)
, how do I filter by the array elements properties?
Another thing is that it seems like the only way to specify position operators is via strings (https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/fundamentals/crud/write-operations/embedded-arrays/#the-first-matching-array-element), isn’t there any utility methods for it yet like in KMongo? Is something like ListDto::categories.allPosOp
planned?