monger
db 연결
(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))
with-collection
(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)))
with-collection 사용시 clj-kondo
관련 문서
Plugin Backlinks: 아무 것도 없습니다.