Skip to content

Commit 4a46964

Browse files
Fix issue on collections with '.'
Merge pull request #147 from pierreMizrahi/master
2 parents 995087e + 51358d1 commit 4a46964

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Database/MongoDB/Internal/Util.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ bitOr = foldl (.|.) 0
9292
-- ^ Concat first and second together with period in between. Eg. @\"hello\" \<.\> \"world\" = \"hello.world\"@
9393
a <.> b = T.append a (T.cons '.' b)
9494

95+
splitDot :: Text -> (Text, Text)
96+
splitDot t = let (pre, post) = T.break (== '.') t in (pre, T.drop 1 post)
97+
9598
true1 :: Label -> Document -> Bool
9699
-- ^ Is field's value a 1 or True (MongoDB use both Int and Bools for truth values). Error if field not in document or field not a Num or Bool.
97100
true1 k doc = case valueAt k doc of

Database/MongoDB/Query.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ import Database.MongoDB.Internal.Protocol
133133
)
134134
import Control.Monad.Trans.Except
135135
import qualified Database.MongoDB.Internal.Protocol as P
136-
import Database.MongoDB.Internal.Util (liftIOE, loop, true1, (<.>))
136+
import Database.MongoDB.Internal.Util (liftIOE, loop, true1, (<.>), splitDot)
137137
import System.Mem.Weak (Weak)
138138
import Text.Read (readMaybe)
139139
import Prelude hiding (lookup)
@@ -1273,7 +1273,7 @@ find q@Query{selection, batchSize} = do
12731273
let newQr =
12741274
case fst qr of
12751275
Req qry ->
1276-
let coll = last $ T.splitOn "." (qFullCollection qry)
1276+
let (_db, coll) = splitDot (qFullCollection qry)
12771277
in (Req $ qry {qSelector = merge (qSelector qry) [ "find" =: coll ]}, snd qr)
12781278
-- queryRequestOpMsg only returns Cmd types constructed via Req
12791279
_ -> error "impossible"
@@ -1333,7 +1333,7 @@ findOne q = do
13331333
let newQr =
13341334
case fst qr of
13351335
Req qry ->
1336-
let coll = last $ T.splitOn "." (qFullCollection qry)
1336+
let (_db, coll) = splitDot (qFullCollection qry)
13371337
-- We have to understand whether findOne is called as
13381338
-- command directly. This is necessary since findOne is used via
13391339
-- runCommand as a vehicle to execute any type of commands and notices.

test/QuerySpec.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ spec = around withCleanDatabase $ do
7676
db thisDatabase `shouldReturn` testDBName
7777
db (useDb anotherDBName thisDatabase) `shouldReturn` anotherDBName
7878

79+
describe "collectionWithDot" $ do
80+
it "uses a collection with dots in the name" $ do
81+
let coll = "collection.with.dot"
82+
_id <- db $ insert coll ["name" =: "jack", "color" =: "blue"]
83+
Just doc <- db $ findOne (select ["name" =: "jack"] coll)
84+
doc !? "color" `shouldBe` (Just "blue")
85+
86+
7987
describe "insert" $ do
8088
it "inserts a document to the collection and returns its _id" $ do
8189
_id <- db $ insert "team" ["name" =: "Yankees", "league" =: "American"]

0 commit comments

Comments
 (0)