How to Write a View that contains Orphaned Documents?

How can a view be written that contains only orphaned documents (not referenced by any others)?

Hypothetical example: DocType=“Book” is the parent with many DocType=“Page” children. How do you create a view of only those Books which have no Pages ---- without using an array of Pages within Book doc. When the last page is deleted, the Book will also be automatically deleted. However, if something goes wrong in the deletion process, a background cleanup function must go find all the orphaned books without pages and delete them.

Hi,
to detect orphans you can either use a view (this consistency check example might be useful https://github.com/sideshowcoder/cb_integrity_check) or a N1QL query.

You can decide to schedule these queries and do the cleanup yourself. Or you can listen to document mutation using something like DCP.

Thank you Laurent – very much appreciated.