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

Kotlin driver : "Invalid $set :: caused by :: an empty object is not a valid value"

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.11.0
    • Component/s: BSON, Codecs, Kotlin
    • Labels:
      None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      Hello,

      I’m using :

      -> Kotlin driver => org.mongodb:mongodb-driver-kotlin-sync:4.11.0
      (the same issue exists in 4.10.2 too)

      -> MongoDB Atlas Dedicated Cluster M10 v6.1

      How to Reproduce

      Here is a very simple code to reproduce (replace XXX, YYY, ZZZ with your values)

      import com.mongodb.client.model.Filters
      import com.mongodb.client.model.UpdateOptions
      import com.mongodb.client.model.Updates
      import com.mongodb.kotlin.client.MongoClient
      import org.bson.Document
      import org.bson.conversions.Bson
      
      fun main(args: Array<String>) {
        val uri = "XXXXXXXXX"
        val databaseName = "YYYYYYYY"
        val collectionName = "ZZZZZZZ"
        val mongoClient = MongoClient.create(uri)
        val db = mongoClient.getDatabase(databaseName)
        val collection = db.getCollection<Document>(collectionName)
      
        val filter = Filters.eq("id", "1234")
      
        val up = Updates.set("myfield", emptyMap<String,String>())
        collection.updateOne(filter, up, UpdateOptions().upsert(true)) // OK
      
        val upList = listOf<Bson>(up)
        collection.updateOne(filter, upList) // FAILED
      } 

      Additional Background

      Doing the same in mongosh :

       

      db.mycoll.updateOne({"id": "1234"}, {$set: { "myfield": {} } })

      => OK

      db.mycoll.updateOne({"id": "1234"}, [{$set: { "myfield": {} } }])

      => FAILED

      but (with $literal)

      db.mycoll.updateOne({"id": "1234"}, [{$set: { "myfield": {$literal: {}} } }])

      => OK

       

      Here is the log of the MongoDB Kotlin driver sent command:

      Command: {"update": "mycoll", "ordered": true, "$db": "testbug", "lsid": {"id": {"$binary": {"base64": "icFsmZQIQi+kINYdFsc5BA==", "subType": "04"}}}, "updates": [{"q": {"id": "1234"}, "u": [{"$set": {"myfield": {}}}]}]}

      It seems it is missing the $literal

       

      Here is the exception trace

      Exception in thread "main" com.mongodb.MongoWriteException: Write operation error on server localhost:27017. Write error: WriteError{code=40180, message='Invalid $set :: caused by :: an empty object is not a valid value. Found empty object at path myfield', details={}}. at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1093) at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:1076) at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:609) at com.mongodb.kotlin.client.MongoCollection.updateOne(MongoCollection.kt:652) at com.mongodb.kotlin.client.MongoCollection.updateOne$default(MongoCollection.kt:651) at app.moovago.TestbugKt.main(testbug.kt:27)
      
      

       

      Thanks for your help!

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            sebastien.perochon@mobiquite.fr Sébastien Pérochon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: