Skip to content

Commit c4fe378

Browse files
committed
Add insert(), insertOne()
1 parent db00d8e commit c4fe378

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.7.0
4+
5+
- Add db.collection.insert() and db.collection.insertOne() functions
6+
37
## 0.6.0
48

59
- Add Date() and ISODate() functions

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject monglorious "0.6.0"
1+
(defproject monglorious "0.7.0"
22
:author "Dave Bauman"
33
:description "Query MongoDB using strings!"
44
:url "https://github.com/baumandm/monglorious"

src/monglorious/transforms.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@
8686
"aggregate"
8787
(fn [_ db] (apply (partial mg-coll/aggregate db collection-name) args))
8888

89+
"insert"
90+
(let [document-or-documents (first args)]
91+
(if (sequential? document-or-documents)
92+
(fn [_ db] (mg-coll/insert-batch db collection-name document-or-documents))
93+
(fn [_ db] (apply (partial mg-coll/insert-and-return db collection-name) args))
94+
))
95+
96+
"insertone"
97+
(fn [_ db] (apply (partial mg-coll/insert-and-return db collection-name) args))
98+
8999
(throw (Exception. (format "Unsupported function: %s." function-name)))))
90100

91101
;; More than one function

test/monglorious/transforms_test.clj

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[clj-time.format :as f]
88
[clj-time.coerce :as c])
99
(:use midje.sweet)
10-
(:import (com.mongodb MongoQueryException)))
10+
(:import (com.mongodb MongoQueryException WriteResult)))
1111

1212
;; Test run-command-transform()
1313
(against-background
@@ -285,3 +285,39 @@
285285
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 1
286286
(execute {} "testdb" "db.timeDocs.find({ time: { $lte: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 2
287287
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-17') } }).count()") => 2))
288+
289+
;; Test collect-command-transform() with insert()
290+
(against-background
291+
[(before :contents
292+
(let [conn (mg/connect)
293+
db (mg/get-db conn "testdb")]
294+
(if db (mc/drop db "documents"))))
295+
(after :contents
296+
(let [conn (mg/connect)]
297+
(mg/drop-db conn "testdb")))]
298+
299+
(fact "Monglorious inserts a document"
300+
(execute {} "testdb" "db.documents.insert({name: 'TEST'})") => #(= "TEST" (get % "name"))
301+
(execute {} "testdb" "db.documents.insert({name: 'DOCUMENT'})") => (fn [_]
302+
(let [conn (mg/connect)
303+
db (mg/get-db conn "testdb")
304+
docs (mc/find-maps db "documents")]
305+
(and (= 2 (count docs))
306+
(= #{"TEST" "DOCUMENT"} (set (map :name docs)))))))
307+
308+
(fact "Monglorious inserts several documents"
309+
(execute {} "testdb" "db.documents.insert([{name: 'BATCH 1'}, {name: 'BATCH 2'}])") => (fn [_]
310+
(let [conn (mg/connect)
311+
db (mg/get-db conn "testdb")
312+
docs (mc/find-maps db "documents")]
313+
(and (= 4 (count docs))
314+
(= #{"TEST" "DOCUMENT" "BATCH 1" "BATCH 2"} (set (map :name docs)))))))
315+
316+
(fact "Monglorious insertOnes a document"
317+
(execute {} "testdb" "db.documents.insertOne({name: 'ONE'})") => #(= "ONE" (get % "name"))
318+
(execute {} "testdb" "db.documents.insertOne({name: 'TWO'})") => (fn [_]
319+
(let [conn (mg/connect)
320+
db (mg/get-db conn "testdb")
321+
docs (mc/find-maps db "documents")]
322+
(and (= 6 (count docs))
323+
(= #{"TEST" "DOCUMENT" "BATCH 1" "BATCH 2" "ONE" "TWO"} (set (map :name docs))))))))

0 commit comments

Comments
 (0)