mongodb - Clojure - Monger Operators - missing multiple field concat and query support with Date conversion -
i have data set on mongo like:
{"month": 9, "year": 2015, "name": "mr a"} {"month": 9, "year": 2015, "name": "mr b"} {"month": 10, "year": 2015, "name": "mr b"} {"month": 11, "year": 2016, "name": "mr b"}
i trying minimum date using monger, without luck.
the best coming distinct month , year using:
(mc/aggregate mongo-connection collection-name [{$group { :_id { :month "$month", :year "$year" } } }]))
which gave result like:
[{"_id":{"year":2016,"month":11}}, {"_id":{"year":2016,"month":10}}, {"_id":{"year":2016,"month":9}}]
and using clojure libraries minimum date. there direct way using monger?
to in monger sort result first year ascending, month ascending, pick first result.
here's modified example documentation:
(ns my.service.server (:refer-clojure :exclude [sort find]) (:require [monger.core :as mg] [monger.query :refer :all])) (let [conn (mg/connect) db (mg/get-db "your-db") coll "your-coll"] (with-collection db coll (find {}) (fields [:year :month]) ;; important use array maps sort (sort (array-map :year 1 :month 1)) (limit 1))
if often, consider adding index collection speed query:
(ns my.app (:require [monger.core :as mg] [monger.collection :as mc])) (let [conn (mg/connect) db (mg/get-db "your-db") coll "your-collection"] ;; create index on multiple fields (will automatically named year_1_month_1 convention) (mc/ensure-index db coll (array-map :year 1 :month 1)))
Comments
Post a Comment