Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5308

Filters support for Dataclass properties

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 5.3.0
    • Affects Version/s: None
    • Component/s: Kotlin
    • None
    • Java Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      That we now support Filters for Dataclass properties (and support annotations from either bson-kotlin eg: @BsonId or @BsonProperty("myString) or bson-kotlinx eg: @SerialName("myString).

      Usage example:

       data class Person(val name: String, val age: Int, val address: List<String>, val results: List<Int>)
      
      val person = Person("Ada", 20, listOf("St James Square", "London", "W1"), listOf(1, 2, 3))
      
      val myFilter = Person::name eq person.name
      
      

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      Yes, in the Serialization section.

      3. Which versions of the driver/connector does this apply to?

      5.3.0 (so after then next release 5.2.0) - Note: other builders are being worked on Updates, Projections etc..

      Show
      1. What would you like to communicate to the user about this feature? That we now support Filters for Dataclass properties (and support annotations from either bson-kotlin eg: @BsonId or @BsonProperty("myString) or bson-kotlinx eg: @SerialName("myString) . Usage example: data class Person(val name: String , val age: Int, val address: List< String >, val results: List<Int>) val person = Person( "Ada" , 20, listOf( "St James Square" , "London" , "W1" ), listOf(1, 2, 3)) val myFilter = Person::name eq person.name 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes, in the Serialization section. 3. Which versions of the driver/connector does this apply to? 5.3.0 (so after then next release 5.2.0) - Note: other builders are being worked on Updates , Projections etc..

      Can we add Filters support for Dataclass properties?

      We have heard from a customer that find() does not support @SerialName annotations, but that insertOne() does. A related community forums ticket as well: https://www.mongodb.com/community/forums/t/nested-data-class-filters/239472.

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

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            ashni.mehta@mongodb.com Ashni Mehta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: