diff --git a/src/document.lisp b/src/document.lisp index 5f6474d..1a03328 100644 --- a/src/document.lisp +++ b/src/document.lisp @@ -230,6 +230,24 @@ the document was generated by the client (as opposed to having been read from th (multiple-value-bind (exists-p key value) (iterator) (when exists-p (add-element key value doc))))) doc))) + +(defun document->ht (doc/docs) + "Convert a document to a hash-table, recursively." + (cond + ((equal (type-of doc/docs) 'document) + (let* ((ht (slot-value doc/docs 'cl-mongo::elements)) + (keys (hash-table-keys ht))) + (dolist (x keys) + (sethash x (document->ht (gethash x ht)) ht)) + ht)) + ((listp doc/docs) + (mapcar #'document->ht doc/docs)) + ((equal (type-of doc/docs) 'cl-mongo::bson-time) + (bson-time-to-ut doc/docs)) + (t doc/docs))) + +(defmacro sethash(key value hash-table) + `(setf (gethash ,key ,hash-table) ,value)) (defun ht->document.1 (ht) (multiple-value-bind (oid oid-supplied) (gethash "_id" ht) diff --git a/src/packages.lisp b/src/packages.lisp index 82ade25..661a66e 100644 --- a/src/packages.lisp +++ b/src/packages.lisp @@ -15,6 +15,7 @@ :collect-all-elements :rm-element :ht->document + :document->ht :mapdoc :doc-id :get-keys