You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: databases/mongo/mongo.asciidoc
+41-31Lines changed: 41 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,54 +13,41 @@ Use Monger to connect to MongoDB and search or manipulate the data. Monger is a
13
13
First, add Monger to your `project.clj` file:
14
14
15
15
[source,clojure]
16
-
---
16
+
----
17
17
(defproject mongo-time "1.0.0"
18
18
:dependencies [[com.novemberain/monger "1.6.0"]])
19
-
---
19
+
----
20
20
21
21
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.
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.
89
76
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.
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.
91
99
92
100
[source,clojure]
93
-
---
101
+
----
94
102
(require '[monger.collection :as coll])
95
103
(import '[org.bson.types ObjectId])
96
104
@@ -100,12 +108,12 @@ When inserting data, giving each document an `_id` is optional. One will be crea
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.
106
114
107
115
[source,clojure]
108
-
---
116
+
----
109
117
(require '[monger.core :as mongo]
110
118
'[monger.multi.collection :as multi])
111
119
@@ -130,11 +138,11 @@ In its idiomatic usage, Monger is set up to work with one connection and one dat
130
138
(multi/insert geo-db "shapes"
131
139
{:name "square" :sides 4
132
140
:parallel true :equal true}))
133
-
---
141
+
----
134
142
135
143
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.
136
144
137
-
---
145
+
----
138
146
(require '[monger.query :as q])
139
147
140
148
;; 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
153
161
(q/with-collection "users"
154
162
(q/find {"$or" [{:state {"$ne" "OK"}}
155
163
{:name #"^S"}]}))
156
-
---
164
+
----
165
+
166
+
===== See Also
157
167
158
168
CongoMongo is another Clojure library for working with MongoDB that you can consider.
0 commit comments