fix code to support arrays of documents