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))

.clj-kondo/config.edn

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

  • open/monger.txt
  • Last modified: 2024/10/05 06:15
  • by 127.0.0.1