Updates.unset() throwing MongoWriteException

My collection:

[
  {
    _id: 1,
    name: "Ann",
    active: false
  },
  {
    _id: 2,
    name: "Bob",
    active: true
  }
]

From my Java application, I want to unset the active field on Bob’s document:

  Bson update = Updates.unset("active");
  collection.updateOne(Filters.eq("_id", 2), update);

But I get a error from MongoCollectionImpl:

com.mongodb.MongoWriteException: Write operation error on server 127.0.0.1:27017. Write error: WriteError{code=31002, message='$unset specification must be a string or an array', details={}}.
	at org.mongodb.driver.sync.client@4.11.1/com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1093)
...

The update Bson printed out looks like:

  Update{fieldName='active', operator='$unset', value=}

I tried constructing a BsonDocument directly using “$unset”, but it gave me the same error:

  Bson update = new BsonDocument("$unset", new BsonDocument("active", new BsonString("")));

I can set the value via Update.set() and I can unset the value via mongosh. Am I doing something incorrectly?

have you managed to solve this issue? I’ve got the exact same error

I am afraid I have not.

I just did. It’s more of a workaround. Instead of using Updates.unset(fieldName)```, I use new Document(“$unset”, fieldName)```