open:monger

monger

(ns clj.db.mongodb
  (:require [monger.core :as mg]
            [monger.collection :as mc]
            [monger.credentials :as mcr]
            [monger.query :as mq]
            [monger.operators :as mo])
  (:import [com.mongodb MongoOptions ServerAddress]
           org.bson.types.ObjectId))
 
(def mongo-db
  (let [^MongoOptions opts (mg/mongo-options {:threads-allowed-to-block-for-connection-multiplier 300})
        ^ServerAddress sa  (mg/server-address mongo-host mongo-port)
        cred               (mcr/create mongo-user mongo-system mongo-password)
        conn               (mg/connect sa opts cred)]
    (mg/get-db conn "monger-test")))

(def documents-coll "documents")
 
(mc/find-maps mongo-db documents-coll {:age 32})
(mc/find-one-as-map mongo-db documents-coll {:age 31})
(mc/find-map-by-id mongo-db documents-coll (ObjectId.))
(mc/find-maps mongo-db "products" {:price_in_subunits {"$gt" 1200 "$lte" 4000}})
(mc/count mongo-db documents-coll)
(mq/with-collection mongo-db documents-coll
  (mq/find {:age {mo/$gte 30}})
  (mq/sort {:age -1})
  (mq/batch-size 5000))

(defn search-by-exclude-regex [db regex page-size book-ids]
  (mq/with-collection db book-coll
    (mq/find {mo/$and [{mo/$or [{:title {mo/$regex   regex
                                         mo/$options "i"}}
                                {:sub-title {mo/$regex   regex
                                             mo/$options "i"}}]}
                       {:_id {mo/$nin book-ids}}]})
    (mq/sort (array-map :sales-index -1 :publish-date -1))
    (mq/paginate :page 1 :per-page page-size)))

.clj-kondo/config.edn

{:linters
 {:invalid-arity
  {:skip-args [monger.query/with-collection]}}}


  • open/monger.txt
  • 마지막으로 수정됨: 2023/06/19 06:12
  • 저자 MORO