From 25a24a523127edeb20dd57d2613c4fc125430551 Mon Sep 17 00:00:00 2001 From: Oleksandr Petrov Date: Sun, 11 Sep 2011 14:40:46 +0200 Subject: [PATCH] Adding basic operations documentation --- examples/basic_operations.clj | 68 ++++++++++++++++++++++++++++++----- src/monger/collection.clj | 11 ++++++ 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/examples/basic_operations.clj b/examples/basic_operations.clj index 209f7cf..867c5b1 100644 --- a/examples/basic_operations.clj +++ b/examples/basic_operations.clj @@ -1,16 +1,68 @@ (ns examples.basic_operations (:gen-class) - (:require [monger.core]) + (:require [monger core collection util]) (:import (com.mongodb Mongo DB)) (:use [clojure.tools.cli])) +;; Make mongodb-settings accessible from the monger.core namespace +(monger.util/with-ns 'monger.core + (def ^:dynamic *mongodb-settings*)) + +(defn fix-paul-maccartneys-name + "Fix Paul McCartney's name" + [] + (let [ paul_mccartney (monger.collection/find-one "people" { :first_name "Raul" }) ] + + + )) (do - (let - [ args *command-line-args* - parsed-args (cli args - (optional ["--port" "Mongodb port" :default 27017]) - (optional ["--host" "Mongodb host" :default "localhost"]) - (optional ["--db-name" :default "monger-example"])) ] - )) + (let [ + args *command-line-args* + parsed-args (cli args + (optional ["--port" "Mongodb port" :default 27017]) + (optional ["--host" "Mongodb host" :default "localhost"]) + (optional ["--db-name" :default "monger-example"])) ] + ;; Define Mongodb connection settings + + (def ^:dynamic *mongodb-settings* parsed-args) + + (monger.util/with-ns 'monger.core + ;; Establish Mongodb connection + (defonce ^:dynamic *mongodb-connection* (monger.core/connect *mongodb-settings*)) + ;; Connect to the database + (defonce ^:dynamic *mongodb-database* (monger.core/get-db "monger-example"))) + + (println "Does people connection exist: " (monger.collection/exists? "people")) + + ;; Insert a record to people collection + (monger.collection/insert "people" { :first_name "John" :last_name "Lennon" }) + + ;; Count an amount of records just inserted + (println "People collection is: " (monger.collection/count "people")) + + ;; Insert several records + (monger.collection/insert-batch "people" [{ :first_name "Ringo" :last_name "Starr" } + { :first_name "Raul" :last_name "McCartney" } + { :first_name "George" :last_name "Harrison" } ]) + + (println "People collection is: " (monger.collection/count "people")) + + ;; Fix a typo in the inserted record + (monger.collection/update "people" { :first_name "Raul" } { "$set" { :first_name "Paul" } }) + + (println (monger.collection/find-one "people" { :first_name "Paul" })) + + ;; Now, let's add the index to that record + (monger.collection/update "people" { :first_name "Paul" } { "$set" { :years_on_stage 1 } }) + + ;; Increment record 45 times + (dotimes [n 45] + (monger.collection/update "people" { :first_name "Paul" } { "$inc" { :years_on_stage 1 } })) + + (println (monger.collection/find-one "people" { :first_name "Paul" })) + + ;; Remove people collection + (monger.collection/drop "people") + )) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 8f63ea4..0fd6ca9 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -130,6 +130,17 @@ ;; monger.collection/update (defn ^WriteResult update + "Please note that update is potentially destructive operation. It will update your document with the given set + emptying the fields not mentioned in (^Map document). In order to only change certain fields, please use + \"$set\", for example: + + (monger.collection/update \"people\" { :first_name \"Raul\" } { \"$set\" { :first_name \"Paul\" } }) + + You can use all the Mongodb Modifier Operations here, as well: + + (monger.collection/update \"people\" { :first_name \"Paul\" } { \"$set\" { :index 1 } }) + (monger.collection/update \"people\" { :first_name \"Paul\" } { \"$inc\" { :index 5 } })" + [^String collection, ^Map conditions, ^Map document, & { :keys [upsert multi write-concern] :or { upsert false, multi false, write-concern monger.core/*mongodb-write-concern* } }] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.update coll (to-db-object conditions) (to-db-object document) upsert multi write-concern)))