Last active
September 16, 2015 14:08
-
-
Save tonsky/498046f745874223885b to your computer and use it in GitHub Desktop.
Querying DataScript database for entities with 3+ tags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[datascript :as d]) | |
(defn count-vals [db e a] | |
(count (d/datoms db :eavt e a))) | |
(let [db (-> (d/empty-db {:link/tags {:db/cardinality :db.cardinality/many}}) | |
(d/db-with [[:db/add 1 :link/tags "a"] | |
[:db/add 1 :link/tags "b"] | |
[:db/add 1 :link/tags "c"] | |
[:db/add 2 :link/tags "x"] | |
[:db/add 2 :link/tags "y"] | |
[:db/add 3 :link/tags "q"] | |
[:db/add 3 :link/tags "p"] | |
[:db/add 3 :link/tags "r"]]))] | |
(d/q '[:find [?e ...] | |
:in $ ?count-vals | |
:where [?e :link/tags] | |
[(?count-vals $ ?e :link/tags) ?vsc] | |
[(>= ?vsc 3)]] | |
db count-vals)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment