[JAVA-5237] Kotlin driver : "Invalid $set :: caused by :: an empty object is not a valid value" Created: 08/Nov/23  Updated: 20/Nov/23  Resolved: 20/Nov/23

Status: Closed
Project: Java Driver
Component/s: BSON, Codecs, Kotlin
Affects Version/s: 4.11.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Sébastien Pérochon Assignee: Ross Lawley
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

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?


 Description   

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!



 Comments   
Comment by Ross Lawley [ 20/Nov/23 ]

Closing as "Won't Fix" as there isn't anything to fix in the driver.

Having tested on the latest MongoDB 7.0 the $literal syntax appears to no longer be required, when using updates with pipelines.

Comment by Ross Lawley [ 20/Nov/23 ]

Hi sebastien.perochon@mobiquite.fr,

It looks like its a MongoDB 6.0 issue. Having tested it on MongoDB 7.0 the code in the shell and via the driver works as expected.

In the meantime please update the Kotlin code to mirror the code used in the shell:

val up = Updates.set("myfield", Document("\$literal", emptyMap<String,String>()))

All the best,

Ross

Comment by PM Bot [ 08/Nov/23 ]

Hi sebastien.perochon@mobiquite.fr, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:04:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.