Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76ecc79

Browse files
committed
Improved Mongo recipe
* Fixed code blocks * Added See Also section * Moved some solution to discussion
1 parent 6fee1ac commit 76ecc79

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

databases/mongo/mongo.asciidoc

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,41 @@ Use Monger to connect to MongoDB and search or manipulate the data. Monger is a
1313
First, add Monger to your `project.clj` file:
1414

1515
[source,clojure]
16-
---
16+
----
1717
(defproject mongo-time "1.0.0"
1818
:dependencies [[com.novemberain/monger "1.6.0"]])
19-
---
19+
----
2020

2121
To connect to MongoDB, use the `monger.core/connect!` function. This will store your connection in the `*mongodb-connection*` dynamic var. If you want to get a connection to use without storing it in a dynamic var, you can use `monger.core/connect` with the same options.
2222

2323
[source,clojure]
24-
---
24+
----
2525
(require '[monger.core :as mongo]))
2626
27-
;; Connects to localhost, port 27017 by default.
28-
(mongo/connect!)
29-
30-
;; Connects to another machine.
31-
(mongo/connect! {:host "192.168.1.100" :port 27017})
27+
;; Connect to localhost.
28+
(mongo/connect! {:host "127.0.0.1" :port 27017})
3229
33-
;; Connects using more complex options.
34-
(let [options (mongo/mongo-options :auto-connect-retry true
35-
:connect-timeout 15
36-
:socket-timeout 15)
37-
server (mongo/server-address "192.168.1.100" 27017)]
38-
(mongo/connect! server options))
39-
40-
;; Connects via a URI.
41-
(mongo/connect-via-uri! (System/genenv "MONGOHQ_URL"))
42-
43-
;; Disconnects.
30+
;; Disconnect when you are done.
4431
(mongo/disconnect!)
45-
---
32+
----
4633

4734
Once you are connected, you can insert and query documents easily.
4835

4936
[source,clojure]
50-
---
37+
----
5138
(require '[monger.core :as mongo]
5239
'[monger.collection :as coll])
5340
(import '[org.bson.types ObjectId])
5441
5542
;; Sets the database in the *mongodb-database* var.
5643
(mongo/use-db! "mongo-time")
5744
58-
;; Inserts one document. :_id is optional.
59-
(coll/insert "users" {:_id (ObjectId.) :name "Pete Killibrew" :state "KY"})
45+
;; Inserts one document.
6046
(coll/insert "users" {:name "Jeremiah Forthright" :state "TX"})
6147
6248
;; Inserts a batch of documents.
63-
(coll/insert-batch "users" [{:name "Wendy Perkins" :state "OK"}
49+
(coll/insert-batch "users" [{:name "Pete Killibrew" :state "KY"}
50+
{:name "Wendy Perkins" :state "OK"}
6451
{:name "Steel Whitaker" :state "OK"}
6552
{:name "Sarah LaRue" :state "WY"}])
6653
@@ -81,16 +68,37 @@ Once you are connected, you can insert and query documents easily.
8168
;; Finds one document and returns it as a Clojure map.
8269
(coll/find-one-as-map "users" {:name "Sarah LaRue"})
8370
;; -> {:_id #<ObjectId 520...>, :state "WY", :name "Sarah LaRue"}
84-
---
71+
----
8572

8673
===== Discussion
8774

8875
MongoDB, especially with Monger, can be a natural choice for storing Clojure data. It stores data as BSON (binary JSON), which maps well to Clojure's own vectors and maps.
8976

77+
There are several ways to connect to Mongo, depending on how much you need to customize your connection and whether you have a map of options or a URI.
78+
79+
[source,clojure]
80+
----
81+
;; Connects to localhost, port 27017 by default.
82+
(mongo/connect!)
83+
84+
;; Connects to another machine.
85+
(mongo/connect! {:host "192.168.1.100" :port 27017})
86+
87+
;; Connects using more complex options.
88+
(let [options (mongo/mongo-options :auto-connect-retry true
89+
:connect-timeout 15
90+
:socket-timeout 15)
91+
server (mongo/server-address "192.168.1.100" 27017)]
92+
(mongo/connect! server options))
93+
94+
;; Connects via a URI.
95+
(mongo/connect-via-uri! (System/genenv "MONGOHQ_URL"))
96+
----
97+
9098
When inserting data, giving each document an `_id` is optional. One will be created for you if you do not have one in your document. It often makes sense to add it yourself, however, if you need to reference the document afterward.
9199

92100
[source,clojure]
93-
---
101+
----
94102
(require '[monger.collection :as coll])
95103
(import '[org.bson.types ObjectId])
96104
@@ -100,12 +108,12 @@ When inserting data, giving each document an `_id` is optional. One will be crea
100108
;; Later, look up your user by their id.
101109
(coll/find-map-by-id "users" id))
102110
;; -> {:_id #<ObjectId 521...>, :name "Lola Morales"}
103-
---
111+
----
104112

105113
In its idiomatic usage, Monger is set up to work with one connection and one database, as `monger.core/connect!` and `monger.core/use-db!` set dynamic vars to hold their information. It is easy to work around this, though. You can use `binding` to set these explicitly around code. In addition, you can use the `monger.multi.collection` namespace instead of `monger.collection`. All functions in the `monger.multi.collection` namespace take a database as their first argument.
106114

107115
[source,clojure]
108-
---
116+
----
109117
(require '[monger.core :as mongo]
110118
'[monger.multi.collection :as multi])
111119
@@ -130,11 +138,11 @@ In its idiomatic usage, Monger is set up to work with one connection and one dat
130138
(multi/insert geo-db "shapes"
131139
{:name "square" :sides 4
132140
:parallel true :equal true}))
133-
---
141+
----
134142

135143
The basic find functions in `monger.collection` will work for simple queries, but you will soon find yourself needing to make more complex queries, which is where `monger.query` comes in. This is a domain-specific language for MongoDB queries.
136144

137-
---
145+
----
138146
(require '[monger.query :as q])
139147
140148
;; Find users, skipping the first two and getting the next three.
@@ -153,6 +161,8 @@ The basic find functions in `monger.collection` will work for simple queries, bu
153161
(q/with-collection "users"
154162
(q/find {"$or" [{:state {"$ne" "OK"}}
155163
{:name #"^S"}]}))
156-
---
164+
----
165+
166+
===== See Also
157167

158168
CongoMongo is another Clojure library for working with MongoDB that you can consider.

0 commit comments

Comments
 (0)