-2

I would try to perform a SQL JOIN like on results from MongoDB. I have data to show on grid:

seqgroup: ({:_id #<ObjectId 54db3ba76a5b2d5de2e7e990>, :group_name gr1, :usersingroup_id [#<ObjectId 54d1e37a2039b30a00b342ca> #<ObjectId 54d8ab240e81f5d1cbf6f691>]}
           {:_id #<ObjectId 54db46ba6a5b2d5de2e7e992>, :group_name gr2, :usersingroup_id [#<ObjectId 54d8af860e81f5d1cbf71da3> #<ObjectId 54d8ab240e81f5d1cbf6f691>]})

Instead of ObjectId for :usersingroup_id I would like to show user name that I keep in different collection with returns array :

get-one-user-byid:  {:_id #<ObjectId 54d1e37a2039b30a00b342ca>, :id someuser1}
get-one-user-byid:  {:_id #<ObjectId 54d8ab240e81f5d1cbf6f691>, :id someuser2}

How I would walk/get/conj/?? or anything else to have seq that contains usernames not ObjectId?

seqresultgroup: ({:_id #<ObjectId 54db3ba76a5b2d5de2e7e990>, :group_name gr1, :usersingroup_id [someuser1 someuser2]}
           {:_id #<ObjectId 54db46ba6a5b2d5de2e7e992>, :group_name gr2, :usersingroup_id [someuser3 someuser2]}) 
marek
  • 57
  • 2
  • 12

1 Answers1

1

If I understand your question correctly, you can try something like this.. We map an anonymous function over the seq of groups that updates the vector of user ID's to a vector of usernames. The userids->usernames function maps an anonymous function that pulls out the :id from the result of your get-one-user-byid function over the vector of ids. This results in the :usersingroupid vector of each group map getting set to a vector of user ID's.

(defn userids->usernames [id-coll]
      (map #(:id (get-one-user-byid %)) id-coll))

(map #(assoc % :usersingroup_id (userids->usernames (:usersingroupid %))) seqgroup)
  1. #(assoc % :usersingroup_id ...)

    • The anonymous function above is "mapped" over the list of "group" maps you call seqgroup. This function will take each of the group maps, %, change the value of the key :usersingroup_id to the result of calling userids->usernames with the value of the key :usersingroup_id in the given group map as an argument.
  2. userids->usernames

    • This function takes the vector of group IDs, and maps an anonymous function over it. The anonymous function uses your get-one-user-byid function to turn the :_id of a user, %, into a user map. We extract the :id from the user map by simply wrapping (get-one-user-byid %) in (:id ...)
  • I am away from my computer and cannot test the code right now, but I will double check the code in an hour or so. –  Feb 25 '15 at 17:37
  • Looks incredibly mysteriously for my human being. I will test it in the morning with a pleasure. – marek Feb 25 '15 at 18:36
  • Obviously it works. I just need to turn: `clojure.lang.LazySeq@74468e66` into something more friendly to show in `html`. For now I will do `(pr-str (map #(:id (db/get-one-user-byid-grid %)) id-coll))` – marek Feb 26 '15 at 09:24