Handling data consistency in a Document Database

You know what an invoice looks like.  We have all seen thousands of them - probably too many. But invoices illustrate an interesting problem - and I am not talking about your VAT  return. I am talking about the new kids on the database block - document databases.  An example is MongoDB. These can store all the data for one invoice in a single structured record (called a 'document').   

In a traditional Relational Database, the data is held in flat tables, like big spreadsheets. Invoices will be stored in two tables, one for information about the invoice, like customer and date (call it the 'invoice header'). The second table will store all the invoice lines. One row per line. The lines will be linked back to their invoice by an invoice number.  But what happens if the invoice header disappears, but the invoice lines are still there?