The query is in the link, but I can paste it here for posterity. I’ve preserved the surrounding clojure which defines multiple queries for context. The query with the JS is assigned to the initial-items variable. The type-alias inside the $map which uses the ternary to return ‘uuid’ or type is the JS that would be nice to replace. I’m neither a clojure developer nor a mongodb expert, so bear with any misinterpretations I may have made.
"To understand how this works, see the comment block below for a rough translation of this query into Clojure."
[& {:keys [collection-name sample-size max-depth]}]
(let [start-n (quot sample-size 2)
end-n (- sample-size start-n)
sample [{"$sort" {"_id" 1}}
{"$limit" start-n}
{"$unionWith"
{"coll" collection-name
"pipeline" [{"$sort" {"_id" -1}}
{"$limit" end-n}]}}]
initial-items [{"$project" {"path" "$ROOT"
"kvs" {"$map" {"input" {"$objectToArray" "$$ROOT"}
"as" "item"
"in" {"k" "$$item.k"
"object" {"$cond" {"if" {"$eq" [{"$type" "$$item.v"} "object"]}
"then" "$$item.v"
"else" nil}}
"type" {"$type" "$$item.v"}
"type-alias" {"$function" {"body" "function(val, type) { return (type == 'binData' && val.type == 4) ? 'uuid' : type; }"
"args" ["$$item.v" {"$type" "$$item.v"}]
"lang" "js"}}}}}}}
{"$unwind" {"path" "$kvs", "includeArrayIndex" "index"}}
{"$project" {"path" "$kvs.k"
"result" {"$literal" false}
"type" "$kvs.type"
"type-alias" "$kvs.type-alias"
"index" 1
"object" "$kvs.object"}}]]
1 Like