Querying MongoDB from Clojure using regex Prefix matches
(:use somnium.congomongo)) (def mongo-server "mongo.test")
(def mongo-db "testdb")
(def mongo-collection "indexedtestcollection") (defn find-test [input]
(mongo!
:host mongo-server
:db mongo-db )
(let [rx (. Pattern compile (str "^" (first input)))]
(println (.getName (.getClass rx)))
(take 3
(fetch mongo-collection
:where {:index rx}
)
)
)
) The main point being that the value for rx must be a Regex instance. In this sample code the regex is dynamically created, using a static #"^regex" works as well. MongoDB can only use the index on the column if the regex is a prefix match (i.e. uses the '^' anchor).
Links:
Clojure (http://clojure.org)
MongoDB: Advanced Queries (http://www.mongodb.org)
CongoMongo (http://)
Clojure (http://clojure.org)
MongoDB: Advanced Queries (http://www.mongodb.org)
CongoMongo (http://)