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!