2016-09-13 4 views
2

내가 좋아하는 몽고의 데이터 세트가 : 어떤없이Clojure의 - 상인 운영자 - 날짜 변환 여러 필드 CONCAT 및 쿼리 지원 누락

{"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"} 

나는이 사용하는 상인의 최소 날짜를 얻기 위해 노력하고 있어요,하지만 운.

내가 할 수있는 최선의

는 별개 월 및 연도 사용을 세우는되었습니다했다

(mc/aggregate mongo-connection collection-name [{$group { :_id { :month "$month", :year "$year" } } }])) 

결과 같은 :

[{"_id":{"year":2016,"month":11}}, 
{"_id":{"year":2016,"month":10}}, 
{"_id":{"year":2016,"month":9}}] 

그리고 나서 최소 날짜를 얻을 Clojure의 라이브러리를 사용하고 있습니다. 몽고를 사용하는 직접적인 방법이 있습니까?

답변

0

monger에서 이것을 수행하려면 먼저 오름차순으로 오름차순으로, 다음으로 오름차순으로 결과를 정렬 한 다음 첫 번째 결과를 선택하십시오. 여기

documentation에서 수정 된 예입니다 :

(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 an index on multiple fields (will be automatically named year_1_month_1 by convention) 
    (mc/ensure-index db coll (array-map :year 1 :month 1))) 
:이 자주 할 무언가 인 경우

(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]) 
    ;; it is VERY IMPORTANT to use array maps with sort 
    (sort (array-map :year 1 :month 1)) 
    (limit 1)) 

쿼리 속도를 컬렉션에 index을 추가하는 것을 고려