Find and Mutate
This operation comes in two forms of mutation, update and remove. Each operation finds documents based on a query and mutates them. After the operation executes it returns the mutates Documents.
This operation finds all Documents matching a query, sorts them and limits the removed results. This function returns an array of the removed Documents.
let adults = try db["users"].findAndRemove("age" >= 18)
The above removes all adult people from the userbase and puts them in an array.
This operation updates the Documents matching a query using an update document. It allows upsertion and can return the Documents in their state before of after the update Document has applied.
let forcedPasswordResets = try db["users"].findAndUpdate("lastPasswordChange" < dataBreachDate, with: [ "$set": [ "requirePasswordUpdate": true, "loginMessage": "Since our website has been hacked, please update your password." ] ], returnedDocument: .old)
The ReturnedDocument enum specifies whether to return the document’s state before or after the
$set operation applied. The options are
.old by default.