MongoDB

Delete

6 samples, from the least code that works to complete workflows.

What a delete does

Rule On MongoDB
By id DeleteAsync(collection, id) returns whether a document went; nothing to delete is false, not an error
By an entity By the declared id, a key or any fields; returns how many went (0 is a normal outcome)
RequireSingleRow None is ARX2002, several ARX2003, and nothing is deleted
Match values A document or array given as a match value is compared as data, never run as an operator
Rows by a condition Delete finds rows by equal values: the id, a declared key, or columns you give values for (ArxMatch.Columns). To delete every row that meets a condition (amount > 100), call DeleteMultipleAsync(table, filter): one call, all or nothing, from 0.1.0-preview.5 (DeleteMultiple, Basics). Before that version: read the rows with RetrieveMultipleAsync and pass them to DeleteMultipleAsync.

The samples

Basics

The least code that works.

1Delete one record by idProgressive

Delete by id, in two scenarios: one document, and one that is already gone (not an error).

2Choose how the documents are foundProgressive

Delete by a key or by any fields, in three scenarios: a compound key, every document with a field's value, and RequireSingleRow, which refuses to delete several.

Real cases

What an application does: a small CRM and product catalogue.

1Catch an injection attemptProgressive

Two attacks on a "delete by" endpoint: a match field named like an operator, and a match value built to match every document. Neither deletes anything.

Advanced cases

Dependency injection, transactions, batch scopes and setting the service up.

1Replace a record in a transactionAdvanced

Replace an order in one ARX transaction: delete the old one, create the new one, commit, in two scenarios: the replacement, and a replacement that fails half way and leaves the old order in place.

2A security check around deletes: probing, blocking, unblocking a false positiveAdvanced

A security check around deletes (SecurityGuard, in the project root), in four scenarios: a delete through an allowed list of types passes; an endpoint that forgot the list is probed, and the attacker is blocked after three attempts; and a false positive, an old client sending a harmless type name, is triaged: support unblocks the user and the endpoint maps the old name.

3Set up the service from the database's schemaAdvanced

The service set up from the database instead of by hand, then deleting through it, in two scenarios: preload the lead collection at startup, and deleting with Strict on, where only the preloaded fields may be named.