Skip to content

Commit 6d27dc5

Browse files
committed
Add ObjectId() support
1 parent d416f6b commit 6d27dc5

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
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.3.0
4+
5+
- Add ObjectId() support
6+
37
## 0.2.1
48

59
- Renamed grammar file to avoid potential conflicts

project.clj

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

resources/monglorious-grammar.ebnf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ roles = <'ROLES'>
1010
profile = <'PROFILE'>
1111
databases = <'DATABASES'>
1212

13-
collection-command = DB DOT collection-name function-application+
13+
collection-command = DB DOT collection-name (DOT function-application)+
1414
<collection-name> = identifier
15-
function-application = DOT function-name LPAREN (literal (COMMA literal)*)? RPAREN
15+
function-application = function-name LPAREN (literal (COMMA literal)*)? RPAREN
1616
<function-name> = identifier
1717

1818
run-command = DB DOT <'RUNCOMMAND'> LPAREN literal RPAREN
1919

2020
(* Common Definitions *)
2121
identifier = #'\$?[a-zA-Z0-9_]+'
22-
<literal> = string | number | map | list | boolean
22+
<literal> = string | number | map | list | boolean | objectid
2323
<string> = single-quoted-string | double-quoted-string
2424
single-quoted-string = #'(?s)\'\'|\'(?:.*?([^\\]|\\\\))?\''
2525
double-quoted-string = #'(?s)\"\"|\"(?:.*?([^\\]|\\\\))\"'
2626
number = #'[-+]?(0(\.\d*)?|([1-9]\d*\.?\d*)|(\.\d+))([Ee][+-]?\d+)?'
27-
2827
list = <'['> (literal (COMMA literal)*)? <']'>
2928
map = <'{'> (map-item (COMMA map-item)*)? <'}'>
3029
<map-item> = (identifier | string) <':'> literal
3130
boolean = TRUE | FALSE
31+
objectid = <'objectid'> LPAREN string RPAREN
3232

3333
<DB> = <'DB'>
3434
<DOT> = <'.'>

src/monglorious/parser.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
(let [keys (take-nth 2 args)
4747
vals (take-nth 2 (rest args))]
4848
(zipmap keys vals)))
49+
:objectid (fn [value] (if (nil? value)
50+
(monger.util/object-id)
51+
(monger.util/object-id value)))
4952
:db-object (fn [db-object] (first db-object))
5053
:function-application (fn [name & args] (into [(lower-case name)] args))
5154
}

test/monglorious/parser_test.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
(parse-query "{x:[1,2,3],y:{a:\"b\"}}" :literal) => [{"x" [1 2 3] "y" { "a" "b"}}]
3737
(parse-query "{x:{y:{z:1}}}" :literal) => [{"x" {"y" {"z" 1}}}])
3838

39+
(fact "Monglorious parses ObjectIds"
40+
(parse-query "ObjectId('581d36e347aee26883837eb7')" :literal) => [(monger.util/object-id "581d36e347aee26883837eb7")]
41+
(parse-query "ObjectId(\"581d36e347aee26883837eb7\")" :literal) => [(monger.util/object-id "581d36e347aee26883837eb7")])
42+
3943
(fact "Monglorious parses SHOW ___ commands"
4044
(parse-query "show dbs") => [:show-command :dbs]
4145
(parse-query "show collections") => [:show-command :collections]

test/monglorious/transforms_test.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
(let [conn (mg/connect)
7878
db (mg/get-db conn "testdb")]
7979
(mc/remove db "documents")
80-
(mc/insert-batch db "documents" [{:name "Alan" :age 27 :score 17772}
80+
(mc/insert-batch db "documents" [{:name "Alan" :age 27 :score 17772 :_id (monger.util/object-id "581d36e347aee26883830000")}
8181
{:name "Joe" :age 32 :score 8277}
8282
{:name "Teresa" :age 31 :score 495044}
8383
{:name "Macy" :age 29 :score 8837777}
@@ -122,6 +122,9 @@
122122
(execute {} "testdb" "db.documents.find({ age: 18 })") => #(and (coll? %) (= 2 (count %)))
123123
(execute {} "testdb" "db.documents.find({ score: 100, child: true })") => #(and (coll? %) (= 1 (count %)) (= "Zoey" (:name (first %)))))
124124

125+
(fact "Monglorious finds documents with ObjectId filters"
126+
(execute {} "testdb" "db.documents.find({ _id: ObjectId('581d36e347aee26883830000') })") => #(and (coll? %) (= 1 (count %)) (= "Alan" (:name (first %)))))
127+
125128
(fact "Monglorious finds documents with complex filters"
126129
(execute {} "testdb" "db.documents.find({ child: { $ne: true }})") => #(and (coll? %) (= 7 (count %)))
127130
(execute {} "testdb" "db.documents.find({ name: { $in: ['Anna', 'Bartleby', 'Xavier', 'Zoey'] }})") => #(and (coll? %) (= 3 (count %))))

0 commit comments

Comments
 (0)