Creating a Type-Safe Query with Kotlin for Document IDs that have a relationship with DBRef
sjiwon opened this issue · comments
Hello, I am developing based on Kotiln + Spring-Data-Mongo.
- Spring Boot: 2.5.0
- Java: 14
- Kotlin: 1.7.0
I left an issue because I had a question while writing Mongo Type-Safe Query using Kotlin
@Document(collection = "origins")
data class Origin(
@Id
val id: String = ObjectId.get().toString(),
@DBRef
val ref: Ref,
)
@Document(collection = "refs")
data class Ref(
@Id
val id: String = ObjectId.get().toString(),
)
Let's assume there's a query like this
@MongoTest
class IssueTest(
private val mongoTemplate: MongoTemplate,
) {
@Test
fun execute() {
val dummyIds = listOf<ObjectId>(
ObjectId("665ad0ac64d2bc76ec965e68"),
ObjectId("665ad0ac64d2bc76ec965e6b"),
ObjectId("665ad0ac64d2bc76ec965e6e"),
)
// QueryA
var criteriaA = Criteria()
criteriaA = criteriaA.and("ref.\$id").inValues(dummyIds)
// QueryB
var criteriaB = Criteria()
criteriaB = criteriaB.and(Origin::ref / Ref::id).inValues(dummyIds)
// execute
println("===== CriteriaA =====")
mongoTemplate.find<Origin>(Query(criteriaA))
println("===== CriteriaB =====")
mongoTemplate.find<Origin>(Query(criteriaB))
}
}
===== CriteriaA =====
16:59:23.465 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - find using query: { "ref.$id" : { "$in" : [{ "$oid" : "665ad0ac64d2bc76ec965e68"}, { "$oid" : "665ad0ac64d2bc76ec965e6b"}, { "$oid" : "665ad0ac64d2bc76ec965e6e"}]}} fields: Document{{}} for class: ...
===== CriteriaB =====
16:59:23.481 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - find using query: { "ref" : { "$in" : [{ "$oid" : "665ad0ac64d2bc76ec965e68"}, { "$oid" : "665ad0ac64d2bc76ec965e6b"}, { "$oid" : "665ad0ac64d2bc76ec965e6e"}]}} fields: Document{{}} for class: ...
The result I want is an inValues query for ref.$id
like CriteriaA.
However, if use the Type-Safe Query provided by Spring-Data-Mongo + Kotlin, will get an inValues Query for ref document, not $id.
Is there a way to access ref.$id using Type-Safe Query?
Did this happen because of the low version?
Thank you @sjiwon for reporting. The issue is not related to the Type-Safe Query but appears to be general one with the mapping of in
targeting dbrefs. "ref.\$id"
works just because it's not targeting a property of the referenced type due to the $
prefix being present.
We'll look into this.
It turns out this issue requires a bit more time. Please use "ref.$id" for the time being.
@christophstrobl
Thank you for your answer. I will try to post the related pr if there is anything else to improve.
To sum it up
- It is the right approach to refer to the collection as "$.id" as
@DBRef
- In the case of "Origin::Ref /Ref::id" in the above code, there is no part mapped to $.id in the data-mongo internal mechanism, so this is recognized as a bug
Is this a flow like this? I'd appreciate it if you could let me know if there's anything wrong.
@christophstrobl
Long time no see.
May I know if any improvements are being made to related bugs?
And I would appreciate it if you could answer the questions I left above.