-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.clj
664 lines (531 loc) · 24.9 KB
/
mongo.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
(ns com.timezynk.mongo
"A functional Clojure wrapper for the modern Java MongoDB API."
^{:doc "A wrapper for the com.mongodb.client Java API.
Requires MongoDB version 4.4 or later."}
(:require
[com.timezynk.mongo.config :refer [*mongo-client* *mongo-database* *mongo-session*]]
[com.timezynk.mongo.methods.aggregate :refer [aggregate-method]]
[com.timezynk.mongo.methods.collation :refer [collation-method]]
[com.timezynk.mongo.methods.connection :refer [connection-method]]
[com.timezynk.mongo.methods.count :refer [count-method]]
[com.timezynk.mongo.methods.create-collection :refer [create-collection-method]]
[com.timezynk.mongo.methods.create-index :refer [create-index-method]]
[com.timezynk.mongo.methods.delete :refer [delete-method delete-one-method]]
[com.timezynk.mongo.methods.drop-collection :refer [drop-collection-method]]
[com.timezynk.mongo.methods.drop-index :refer [drop-index-method]]
[com.timezynk.mongo.methods.fetch :refer [fetch-method]]
[com.timezynk.mongo.methods.fetch-and-delete :refer [fetch-and-delete-method]]
[com.timezynk.mongo.methods.fetch-and-replace :refer [fetch-and-replace-method]]
[com.timezynk.mongo.methods.fetch-and-update :refer [fetch-and-update-method]]
[com.timezynk.mongo.methods.insert :refer [insert-method insert-options]]
[com.timezynk.mongo.methods.list-collections :refer [list-collections-method]]
[com.timezynk.mongo.methods.list-databases :refer [list-databases-method]]
[com.timezynk.mongo.methods.modify-collection :refer [modify-collection]]
[com.timezynk.mongo.methods.replace :refer [replace-method]]
[com.timezynk.mongo.methods.update :refer [update-method update-one-method]]
[com.timezynk.mongo.utils.collection :as coll]
[com.timezynk.mongo.utils.convert :as convert])
(:import [com.mongodb MongoClientSettings]
[com.mongodb.client ClientSession TransactionBody]
[com.mongodb.client.model Collation]))
; ------------------------
; Connection
; ------------------------
(defn create-connection!
"Create a connection object.
| Parameter | Description |
| --- | --- |
| `uri` | `string` Database location. |
| `:retry-writes?` | `optional boolean` Sets whether writes should be retried if they fail due to a network error. Default is `false`. |
| `:write-concern` | `optional enum` Set write concern: |
| | `:acknowledged` Write operations that use this write concern will wait for acknowledgement. Default. |
| | `:majority` Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation. |
| | `:unacknowledged` Write operations that use this write concern will return as soon as the message is written to the socket. |
| | `:w1` Write operations that use this write concern will wait for acknowledgement from a single member. |
| | `:w2` Write operations that use this write concern will wait for acknowledgement from two members. |
| | `:w3` Write operations that use this write concern will wait for acknowledgement from three members. |
**Returns**
The connection object.
**Examples**
```Clojure
; Create a connection with default options
(connection \"mongodb://localhost:27017\")
; Create a custom connection
(connection \"mongodb://localhost:27017\" :retry-writes true :write-concern :w2)
```"
{:arglists '([<uri> & :retry-writes? <boolean> :write-concern [:acknowledged :unacknowledged :journaled :majority :w1 :w2 :w3]])}
^MongoClientSettings [^String uri & options]
(connection-method uri options))
(defn close-connection!
[conn]
(.close (:client conn)))
(defmacro with-mongo
"Functionally set up or change mongodb connection. Reverts to earlier settings when leaving scope.
| Parameter | Description |
| --- | --- |
| `uri` | `string` Connection string. See the [API documentation](http://mongodb.github.io/mongo-java-driver/4.5/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html) for more details. |
| `connection` | A connection object. |
| `body` | Encapsulated program utilizing the connection. |
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(with-mongo \"mongodb://localhost:27017/my-database\"
(insert! :users {:name \"My Name\"})
(fetch! :users))
```"
{:arglists '([<uri> & <body>]
[<connection> & <body>])}
[conn & body]
`(let [conn# ~conn
client# (if (= (type conn#) String)
(connection-method conn# [])
conn#)]
(binding [*mongo-client* (:client client#)
*mongo-database* (:database client#)]
(try
~@body
(finally
(when (= (type conn#) String)
(.close *mongo-client*)))))))
; ------------------------
; Database
; ------------------------
(defmacro with-database
"Functionally set up or change database. Reverts to earlier settings when leaving scope.
| Parameter | Description |
| --- | --- |
| `database` | `string` Name of database to use. |
| `body` | Encapsulated program calling the database. |
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(with-database \"my-database-2\"
(insert! :users {:name \"My Name\"})
(fetch! :users))
```"
{:arglists '([<database> & <body>])}
[db & body]
`(binding [*mongo-database* (.getDatabase *mongo-client* ~db)]
~@body))
(defn list-databases
"List databases for this connection.
**Returns**
A lazy sequence of database objects."
[]
(-> (list-databases-method)
(convert/it->clj)))
; ------------------------
; Collation
; ------------------------
(defn collation
"Create collation.
| Parameter | Description |
| --- | --- |
| `locale` | `string` The two-letter ICU locale string. |
| `:alternate` | `optional enum` Should whitespace and punctuation be considered as base characters for purposes of comparison? |
| | `:non-ignorable` Whitespace and punctuation are considered base characters. |
| | `:shifted` Whitespace and punctuation are not considered base characters and are only distinguished at strength levels greater than 3. |
| `:backwards?` | `optional boolean` Whether strings with diacritics sort from back of the string, such as with some French dictionary ordering. Default is `false`. |
| `:case-first` | `optional enum` Sort order of case differences during tertiary level comparisons. |
| | `:lower` Uppercase sorts before lowercase. |
| | `:upper` Lowercase sorts before uppercase. |
| | `:off` Default value. Similar to `:lower` with slight differences. |
| `:case-level?` | `optional boolean` Flag that determines whether to include case comparison at strength level 1 or 2. |
| `:max-variable` | `optional enum` Which characters are considered ignorable when `:alternate = :shifted`? Has no effect if `:alternate = :non-ignorable`. |
| | `:punct` Both whitespace and punctuation are ignorable and not considered base characters. |
| | `:space` Whitespace is ignorable and not considered to be base characters. |
| `:normalization?` | `optional boolean` Check if text requires normalization and to perform normalization. Default is `false`. |
| `:numeric-ordering?` | `optional boolean` Compare numeric strings as numbers or as strings. Default is `false`. |
| `:strength` | `optional enum` The level of comparison to perform. |
| | `:identical` Limited for specific use case of tie breaker. |
| | `:primary` Collation performs comparisons of the base characters only, ignoring other differences such as diacritics and case. |
| | `:secondary` Collation performs comparisons up to secondary differences, such as diacritics. That is, collation performs comparisons of base characters (primary differences) and diacritics (secondary differences). Differences between base characters takes precedence over secondary differences. |
| | `:tertiary` Collation performs comparisons up to tertiary differences, such as case and letter variants. That is, collation performs comparisons of base characters (primary differences), diacritics (secondary differences), and case and variants (tertiary differences). Differences between base characters takes precedence over secondary differences, which takes precedence over tertiary differences. Default level. |
| | `:quaternary` Limited for specific use case to consider punctuation when levels 1-3 ignore punctuation or for processing Japanese text. |
For more details, see the [manual page on collation](https://www.mongodb.com/docs/v5.3/reference/collation/).
**Returns**
The collation object.
**Examples**
```Clojure
(collation)
```"
{:arglists '([<locale> & :alternate [:non-ignorable :shifted] :backwards? <boolean> :case-first [:lower :off :upper]
:case-level? <boolean> :max-variable [:punct :space] :normalization? <boolean> :numeric-ordering? <boolean>
:strength [:identical :primary :quaternary :secondary :tertiary]])}
^Collation [locale & options]
(collation-method locale options))
; ------------------------
; Collection
; ------------------------
(defn list-collections
"List full info of all collections in database."
[]
(-> (list-collections-method *mongo-database*)
(convert/it->clj)))
(defn list-collection-names
"List keyworded names of all collections in database."
[]
(->> (list-collections)
(map :name)
(map keyword)))
(defn collection-info
"List full info of collection.
| Parameter | Description |
| --- | --- |
| `name` | `keyword/string` Collection name. |"
[coll]
(->> (list-collections)
(filter #(= (name coll) (:name %)))
(first)))
(defn create-collection!
"Create collection.
| Parameter | Description |
| --- | --- |
| `name` | `keyword/string` Collection name. |
| `:collation` | `optional collation object` The collation of the collection. |
| `:schema` | `optional map` The schema validation map. |
| `:validation` | `optional map` Validation logic outside of the schema. |
| `:level` | `optional enum` Validaton level: |
| | `:strict` Apply validation rules to all inserts and all updates. Default value. |
| | `:moderate` Applies validation rules to inserts and to updates on existing valid documents. |
| | `:off` No validation for inserts or updates. |
**Returns**
The collection object.
**Examples**
```Clojure
; Collection with exactly one required field `name` of type `string`:
(create-collection! :users :schema {:name (string)})
; Collection where each document can have either a `name` field or an `address` field, but not both:
(create-collection! :users :validation {:$or [{:name {:$ne nil} :address {:$exists 0}}
{:name {:$exists 0} :address {:$ne nil}}]})
```"
{:arglists '([<name> & :collation <collation object> :level <integer> :schema {} :validation {}])}
[coll & options]
(create-collection-method (name coll) options))
(defn modify-collection!
"Make updates to a collection.
| Parameter | Description |
| --- | --- |
| `name` | `keyword/string` Collection name. |
| `:name` | `optional keyword/string` New name. |
**Returns**
The collection object.
**Examples**
```Clojure
(modify-collection! :coll :name :coll-2)
```"
{:arglists '([<name> & :name <string>])}
[coll & options]
(modify-collection (coll/get-collection coll) options))
(defn drop-collection! [coll]
(drop-collection-method (coll/get-collection coll)))
; ------------------------
; Index
; ------------------------
(defn list-indexes [coll]
(-> (.listIndexes (coll/get-collection coll))
(convert/it->clj)))
(defn create-index!
"Create an index for a collection.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` Collection name. |
| `keys` | `map/list(keyword/string)` A document or a list of keywords or strings. |
| `:collation` | `optional collation object` Collation of index. |
| `:background?` | `optional boolean` Create the index in the background. Default `false`. |
| `:name` | `optional string` A custom name for the index. |
| `:filter` | `optional map` A partial-filter-expression for the index. |
| `:sparse?` | `optional boolean` Don't index null values. Default `false`. |
| `:unique?` | `optional boolean` Index values must be unique. Default `false`. |
**Returns**
The index name.
**Examples**
```Clojure
; Index over field-1 in descending order, field-2 as hashed
(create-index! :coll {:field-1 -1 :field-2 \"hashed\"})
; Shorthand for indexing over fields in ascending order
(create-index! :coll [:field-1 :field-2])
; Only flagged documents are indexed and searchable
(create-index! :coll [:field-1] :filter {:flag-field true})
```"
{:arglists '([<collection> <keys> & :collation <collation object> :background? <boolean> :name <string> :filter {} :sparse? <boolean> :unique? <boolean>])}
[coll keys & options]
(create-index-method (coll/get-collection coll)
(if (map? keys)
(convert/clj->doc keys)
(convert/list->doc keys))
options))
(defn drop-index! [coll index]
(drop-index-method (coll/get-collection coll)
index))
; ------------------------
; Fetch
; ------------------------
(defn fetch
"Fetch documents from collection.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `:collation` | `optional collation object` Collation used. |
| `:limit` | `optional int` Number of documents to fetch. |
| `:only` | `optional map` A MongoDB map of fields to include or exclude. |
| `:skip` | `optional int` Number of documents to skip before fetching. |
| `:sort` | `optional map` A MongoDB map of sorting criteria. |
**Returns**
A lazy sequence of matching documents.
**Examples**
```Clojure
; Fetch five documents from collection :users
(fetch :users {} :limit 5)
```"
{:arglists '([<collection>]
[<collection> <query> & :collation <collation object> :limit <count> :only {} :skip <count> :sort {}])}
([coll] (fetch coll {}))
([coll query & options] (-> (fetch-method (coll/get-collection coll)
(convert/clj->doc query)
options)
(convert/it->clj))))
(defn fetch-one
"Return only the first document retrieved.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
A single document or `nil`."
{:arglists '([<collection>] [<collection> <query>])}
([coll] (fetch-one coll {}))
([coll query] (-> (fetch coll query :limit 1)
(first))))
(defn fetch-count
"Count the number of documents returned.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
Number of matching documents."
{:arglists '([<collection>] [<collection> <query>])}
([coll] (fetch-count coll {}))
([coll query] (count-method (coll/get-collection coll)
(convert/clj->doc query))))
; ------------------------
; Insert
; ------------------------
(defn insert!
"Insert one document or a list thereof in a collection. Inserting a list is atomic.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `document` | `map` A document. |
| `document-list` | `list(map)` A list of documents. |
| `:write-concern` | `optional enum` Set write concern: |
| | `:acknowledged` Write operations that use this write concern will wait for acknowledgement. Default. |
| | `:majority` Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation. |
| | `:unacknowledged` Write operations that use this write concern will return as soon as the message is written to the socket. |
| | `:w1` Write operations that use this write concern will wait for acknowledgement from a single member. |
| | `:w2` Write operations that use this write concern will wait for acknowledgement from two members. |
| | `:w3` Write operations that use this write concern will wait for acknowledgement from three members. |
**Returns**
The document/s with `_id` fields, either a single document or a lazy sequence.
**Examples**
```Clojure
(insert! :users {:name \"Alice\"})
(insert! :users [{:name \"Alice\"} {:name \"Bob\"}])
```"
{:arglists '([<collection> <document> & :write-concern [:acknowledged :unacknowledged :journaled :majority :w1 :w2 :w3]]
[<collection> <document-list> & :write-concern [:acknowledged :unacknowledged :journaled :majority :w1 :w2 :w3]])}
[coll doc & options]
(let [doc (convert/clj->doc doc)]
(-> (coll/get-collection coll)
(insert-options options)
(insert-method doc))
(convert/doc->clj doc)))
; ------------------------
; Update
; ------------------------
(defn update!
"Update matching documents.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `update` | `map` A valid update document. Must use `$set` or `$push`, throws exception otherwise. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <number of matching documents>
:modified-count <number of modified documents>}
```
**Examples**
```Clojure
(update!)
```"
{:arglists '([<collection> <query> <update> & :upsert? <boolean>])}
[coll query update & options]
(let [result (update-method (coll/get-collection coll)
(convert/clj->doc query)
(convert/clj->doc update)
options)]
{:matched-count (.getMatchedCount result)
:modified-count (.getModifiedCount result)}))
(defn update-one!
"Update first matching document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `update` | `map` A valid update document. Must use `$set` or `$push`, throws exception otherwise. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <0 or 1>
:modified-count <0 or 1>}
```
**Examples**
```Clojure
(update-one!)
```"
{:arglists '([<collection> <query> <update> & :upsert? <boolean>])}
[coll query update & options]
(let [result (update-one-method (coll/get-collection coll)
(convert/clj->doc query)
(convert/clj->doc update)
options)]
{:matched-count (.getMatchedCount result)
:modified-count (.getModifiedCount result)}))
(defn fetch-and-update-one!
"Update first matching document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `return-new?` | `optional boolean` Return the updated document? Default if `false`. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
A single document or nil."
{:arglists '([<collection> <query> & :return-new? <boolean> :upsert? <boolean>])}
[coll query update & options]
(-> (fetch-and-update-method (coll/get-collection coll)
(convert/clj->doc query)
(convert/clj->doc update)
options)
(convert/doc->clj)))
; ------------------------
; Replace
; ------------------------
(defn replace-one!
"Replace a single document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `document` | `map` The new document. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <0 or 1>
:modified-count <0 or 1>}
```"
{:arglists '([<collection> <query> <document> & :upsert? <boolean>])}
[coll query doc & options]
(let [result (replace-method (coll/get-collection coll)
(convert/clj->doc query)
(convert/clj->doc doc)
options)]
{:matched-count (.getMatchedCount result)
:modified-count (.getModifiedCount result)}))
; TODO: test
(defn fetch-and-replace-one! [coll query doc & options]
(-> (fetch-and-replace-method (coll/get-collection coll)
(convert/clj->doc query)
(convert/clj->doc doc)
options)
(convert/doc->clj)))
; ------------------------
; Delete
; ------------------------
(defn delete!
"Delete matching documents.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
```Clojure
{:deleted-count <number of matching documents>}
```"
{:arglists '([<collection> <query>])}
[coll query & options]
(let [result (delete-method (coll/get-collection coll)
(convert/clj->doc query)
options)]
{:deleted-count (.getDeletedCount result)}))
(defn delete-one!
"Delete first matching document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
```Clojure
{:deleted-count <0 or 1>}
```"
{:arglists '([<collection> <query>])}
[coll query & options]
(let [result (delete-one-method (coll/get-collection coll)
(convert/clj->doc query)
options)]
{:deleted-count (.getDeletedCount result)}))
; TODO: test
(defn fetch-and-delete-one! [coll query]
(-> (fetch-and-delete-method (coll/get-collection coll)
(convert/clj->doc query))
(convert/doc->clj)))
; ------------------------
; Transaction
; ------------------------
(defmacro transaction
"Functionally perform a transaction. Encapsulated database requests are queued and then
atomically executed when the function goes out of scope.
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(transaction
(insert! :users {:name \"My Name\"})
(fetch! :users))
```"
[& body]
`(binding [*mongo-session* ^ClientSession (.startSession *mongo-client*)]
(let [txn-body# (reify TransactionBody (execute [_this] ~@body))]
(try
(.withTransaction *mongo-session* txn-body#)
(finally
(.close *mongo-session*))))))
; ------------------------
; Aggregate
; ------------------------
(defn aggregate
"MongoDB aggregation.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` Collection name. |
| `pipeline` | The request pipeline queries. |
**Returns**
Aggregation result.
**Examples**
```Clojure
(aggregate :users
{:$match {:age {:$gte 20}}}
{:$project {:_id 0
:name 1}})
```"
{:arglists '([<collection> & <pipeline>])}
[coll & pipeline]
(-> (aggregate-method (coll/get-collection coll)
(convert/clj->doc pipeline))
(convert/it->clj)))