Find and delete many records atomically using the go driver?

I’d like to find and delete many records. In the console, I’d do this with db.collection.findAndModify, but for some reason the go driver has this method as private. The FindOneAndModify, FindOneAndDelete, and FindOneAndReplace methods all internally call the private findAndModify method … but they all limit things to a single result.

What is the proper way to atomically find and delete many records using the go driver? A session seems like the right direction, but reading the causal consistency guarantees, but even in the write follows reads model, isolation is not guaranteed (that is, another session can interleave between my read and delete).

The right way to atomically delete batches of records is to wrap them in a transaction. My colleague @nraboy has written a tutorial on transactions in GoLang to help you set this up.

1 Like