Toggle Boolean positional operator


I’ve got a nested document which has a boolean as a property and I’d like to toggle it.

I’ll attach a screenshot of the document along with the code that I am currently using

				{ _id: id, "": "Zibby" },
				{ $set: { "items.$.bought": true } }

I have tried these solutions that I found on stackoverflow:

				{ _id: id, "": "Zibby" },
				{ $set: { "items.$.bought": { $not: "items.$.bought" } } }

I get the following error:

MongoServerError: Invalid $set :: caused by :: FieldPath field names may not start with ‘$’.

			.findOneAndUpdate({ _id: id, "": "Zibby" }, [
				{ $set: { "items.$.bought": { $eq: [false, "items.$.bought"] } } },

With the same error

1 Like

I have been looking for a solution to this without using the new $getField but to no avail. “Even the sun has got spots.”

The original author and I would certainly appreciate if someone can pitch in.