The generic and portable way to do this on XQuery engines is:
document-uri( root($z) )
will give you the URI of the document in eXist-db; the collection information is in the URI;
the eXist-db specific way (warning: not portable to other XQuery implementations) is:
util:collection-name( $z )
in the http://exist-db.org/xquery/util
function namespace.