Last Updated: February 25, 2016
·
1.346K
· timgluz

Monger: long running queries without timeout exceptions

I just had problems with running long-time queries using monger - mongodb library for clojure without getting cursor timeout exceptions after ~10 minutes.

Here is my current workaround, until my pull-requests with updated interfaces for find-functions gets accepted:

monger.collection/find function returns DBCursor object, which has public method setOptions, that accepts constants from com.mongodb.Bytes class.


 (ns changelogger.db
    (:require [monger.core :as mongo]
                  [monger.collection :as coll]
                  [monger.conversion :refer [from-db-object]]
                  [monger.joda-time])
   (:import [com.mongodb Bytes]))
...

 (defn get-sorted-products 
   "returns lazy-seq of products-map."
   [the-criteria]
   (let [db-cur (coll/find "products" the-criteria)]
     (.setOptions db-cur Bytes/QUERYOPTION_NOTIMEOUT)
     (map #(from-db-object %1 true) db-cur))

Valid values for setOptions on mongodb docs