Golang driver: $set: an empty object is not a valid value

I’m trying to write a $set query which contain empty values.
The golang bson command is pretty simple:

update := bson.A{
	bson.D{{"$set", bson.D{
		{"profile", bson.D{
			{"fieldName", bson.D{}},
		}},
	}}},
}

The error => Invalid $set :: caused by :: an empty object is not a valid value. Found empty object at path profile.fieldName

I’v search the internet for a solution without luck. I’ve discovered that this should work on any mongodb server since version 5.0. My server is at version 6.0

The exact same query works fine when using the mongosh console inside Mongo Compass on the same server.

Did I miss something ?

Ok ! I found my issue, I need to remove the bson.A wrapped around my bson.D object. The bson.A cause the $set to be treated as an aggregation command, which does not support empty objects.
See: MongoServerError: Invalid $set :: caused by :: an empty object is not a valid value - #4 by Jason_Tran

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.