MongoServerError: Invalid $set :: caused by :: an empty object is not a valid value

const blog = await findAndUpdateBlog({ _id: id }, [
		$set: {
			content: {}, // <-- empty object causing error
			publishedTitle: req.body.title,
			publishedContent: { $literal: req.body.content },
			userId: req.user?._id,
			status: 'published',
			publishedAt: {
				$ifNull: ['$publishedAt', new Date().toISOString()]

Any workaround without using $literal projection.

Hi @Akash_Kumar6,

Welcome to the community!

Please note that in MongoDB Version 5.0, the update operator allows $set to contain an empty object as per the documentation:

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $set with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

However if the empty $set is within an aggregation, there is currently a feature request in SERVER ticket SERVER-54046 which you may watch, upvote or comment on providing your use case which uses an empty $set within the aggregation.

As a workaround, you may want to cater for the empty $set in your aggregation within your code. Alternatively if it suits your use case, you could possibly set the value of the content field to null or undefined instead of an empty object.

Hope this helps.


1 Like

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