added applyDocumentKey, and improved toObject()