cl-mongo
cl-mongo copied to clipboard
Search by oid
I was trying to find a document in a collection using its oid with(imho) the most obvious way: (db.find "collection" (kv "_id" (doc-id somedoc))) But ended up with nothing, so after searching through the sources I've found make-bson-oid function which makes my task possible: (db.find "collection" (kv "_id" (make-bson-oid :oid oid-vector))) So I may be missing something or it will be a good idea to change db.find method for the sake of oid search or maybe just export make-bson-oid?
On the same subject, it would be nice to have a way of converting an oid to a string (similar to the hash that's queryable in the database itself), rather than a set of bytes. Makes it possible to do things such as using the oid as a resource ID in a RESTful service, for example
Picking up on the oid to string point would something like below work? Or is it completely horrible/buggy to do this:
(defun oid->string (oid)
(subseq (string-downcase
(cl-ppcre:regex-replace-all "[#( )]" (format nil "~X" oid) ""))
0 24))
For my use I need to reference a doc from the property of another doc (akin to a foreign key). I couldn't see a straightforward way to accomplish that without using something like the above? (Sorry to piggy back the thread).
This may be of use to people: https://github.com/orthecreedence/cl-mongo-id Not sure if this qualifies as a quick-fix, or a permanent solution, but seems to work well enough.