user3096185
user3096185

Reputation: 23

does Haskell support MongoDB's query operators such as "$in"?

To retrieve a set of documents whose certain field such as author is any one of a set of values, how does haskell's mongodb do that? as it is not apparent to me. Thanks for any hint and help!

Upvotes: 2

Views: 152

Answers (1)

Yuras
Yuras

Reputation: 13876

If you are using mongoDB package, then notice that it accepts arbitrary document as a Selector. So you are free to use anything mongodb supports, including $in:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}

import Database.MongoDB
import Control.Monad.IO.Class (liftIO)

main = do
  pipe <- runIOE $ connect $ host "127.0.0.1"
  e <- access pipe master "testdb" $ do
    insertMany "test" [
      ["i" =: 1, "name" =: "Name1"],
      ["i" =: 2, "name" =: "Name2"],
      ["i" =: 3, "name" =: "Name3"]
      ]
    rest =<< find (select ["i" =: ["$in" =: [1, 3]]] "test")
  close pipe
  print e

Output:

Right [[ _id: 52a9ea3008d0cf401e000000, i: 1, name: "Name1"],[ _id: 52a9ea3008d0cf401e000002, i: 3, name: "Name3"]]

Happy hacking with haskell and mongodb :)

Upvotes: 2

Related Questions