Skip to content
New issue

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

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

Already on GitHub? # to your account

Creating a Type-Safe Query with Kotlin for Document IDs that have a relationship with DBRef #4713

Open
sjiwon opened this issue Jun 1, 2024 · 4 comments
Assignees
Labels
status: pending-design-work Needs design work before any code can be developed type: bug A general bug

Comments

@sjiwon
Copy link

sjiwon commented Jun 1, 2024

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?

@christophstrobl
Copy link
Member

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.

@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 18, 2024
@christophstrobl
Copy link
Member

It turns out this issue requires a bit more time. Please use "ref.$id" for the time being.

@christophstrobl christophstrobl added the status: pending-design-work Needs design work before any code can be developed label Jun 20, 2024
@sjiwon
Copy link
Author

sjiwon commented Jun 30, 2024

@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.

@sjiwon
Copy link
Author

sjiwon commented Jul 21, 2024

@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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants