monger/examples/basic_operations.clj

83 lines
3 KiB
Clojure
Raw Normal View History

2011-09-08 20:27:49 +00:00
(ns examples.basic_operations
(:gen-class)
2011-09-11 12:40:46 +00:00
(:require [monger core collection util])
(:import (com.mongodb Mongo DB DBObject))
2011-09-08 20:27:49 +00:00
(:use [clojure.tools.cli]))
2011-09-11 12:40:46 +00:00
(defn fix-paul-maccartneys-name
"Fix Paul McCartney's name"
[]
(let [ paul_mccartney (monger.collection/find-one "people" { :first_name "Raul" }) ]
))
2011-09-08 20:27:49 +00:00
(do
2011-09-11 12:40:46 +00:00
(let [
args *command-line-args*
parsed-args (cli args
2012-02-26 17:45:32 +00:00
["--port" "Mongodb port" :default 27017]
["--host" "Mongodb host" :default "127.0.0.1"]
["--db-name" :default "monger-example"]) ]
2011-09-11 12:40:46 +00:00
2012-02-26 17:45:32 +00:00
(monger.core/connect! (first parsed-args))
(monger.core/set-db! (monger.core/get-db "monger-example"))
2011-09-11 12:40:46 +00:00
(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" } })
2012-02-26 17:45:32 +00:00
(println (monger.collection/find-one-as-map "people" { :first_name "Paul" }))
2011-09-11 12:40:46 +00:00
;; Now, let's add the index to that record
2012-02-26 17:45:32 +00:00
(monger.collection/update "people" { :first_name "Paul" } { "$set" { :years_on_stage 1 } })
(println (monger.collection/find-one-as-map "people" { :first_name "Paul" }))
2011-09-11 12:40:46 +00:00
;; Increment record 45 times
(dotimes [n 45]
2012-02-26 17:45:32 +00:00
(monger.collection/update "people" { :first_name "Paul" } { "$inc" { :years_on_stage 1 } })
(println (monger.collection/find-one-as-map "people" { :first_name "Paul" }))
)
2011-09-11 12:40:46 +00:00
;; Remove years_on_stage field
(monger.collection/update "people" { :first_name "Paul" } { "$unset" { :years_on_stage 1} })
;; Insert the record to the data set if it wasn't there yet
(monger.collection/update "people" { :first_name "Yoko" } { :first_name "Yoko" :last_name "Ono" } :upsert true)
;; Update multiple records
(monger.collection/update "people" { } { "$set" { :band "The Beatles" }} :multi true)
;; Save can act both like insert and update
(def ian_gillian
2012-02-26 17:45:32 +00:00
(monger.conversion/to-db-object
{ :first_name "Ian" :last_name "Gillan" }))
;; Performs insert
(monger.collection/save "people" ian_gillian)
;; Performs update
(monger.collection/save "people"
{ :_id (monger.util/get-id ian_gillian)
:first_name "Ian"
:last_name "Gillan" :band "Deep Purple" })
2011-09-08 20:27:49 +00:00
2011-09-11 12:40:46 +00:00
;; Remove people collection
(monger.collection/drop "people")
))