Invert bool value with update

Hi everybody!

I have a document with a bool value, which I want to invert. How would I do that with golang?
I already established that I my update function works. I just don’t know which command to use.

Thank you in advance!

Hi @Tripple_U,

Sorry for the late reply but here is a solution written in the Mongo Shell which should be easy to transform into Golang ─ but sadly, I don’t speak golang yet :slight_smile:.

test:PRIMARY> db.coll.insertOne({bool:true})
	"acknowledged" : true,
	"insertedId" : ObjectId("6075cfffdf48fa5c4d46e8ae")
test:PRIMARY> db.coll.findOne()
{ "_id" : ObjectId("6075cfffdf48fa5c4d46e8ae"), "bool" : true }
test:PRIMARY> db.coll.updateOne({"_id" : ObjectId("6075cfffdf48fa5c4d46e8ae")}, [{"$set": {bool: {"$not": "$bool"}}}])
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
test:PRIMARY> db.coll.findOne()
{ "_id" : ObjectId("6075cfffdf48fa5c4d46e8ae"), "bool" : false }

Note the important trick here: we are using the Aggregation Pipeline update syntax to use the $set and get access to the current value with $bool.