Haskell Mongodb从文件中插入记录

时间:2017-03-09 08:47:47

标签: mongodb haskell

我尝试编写一个程序来读取文件并将信息存储在mongoldb集合中。该程序基于mongodb驱动程序的示例程序。 对于测试,我在testdaten中定义了一些常量,程序使用这些数据。当我尝试插入文件数据时,我得到一个类型错误:

insertMany" daten" getWetterDaten   无法匹配预期类型[Document]             实际类型IO [[Data.Bson.Field]]   在insertMany的第二个参数中,即getWetterDaten   在do块的stmt中:insertMany "daten" getWetterDaten

我知道问题是testdaten有Type testDaten :: [[Data.Bson.Field]] 并且getWetterDatenType getWetterDaten :: IO [[Data.Bson.Field]], 但我怎么能解决这个问题?

当我尝试将数据作为参数传递给insertData2时,我收到消息:

Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
                   Database.MongoDB.Query.MongoContext m1 [Value]’

这绝对远远超出了我对Haskell的理解。

另一个观察是,如果我尝试包含类型声明 我收到一条错误消息:

Not in scope: type constructor or class `Data.Bson.Field`

将文件数据插入mongodb的正确方法是什么?

  {-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE ExtendedDefaultRules #-}
  module TestModule where

  import Database.MongoDB    (Action, Document, Document, Value, access,
                              close, connect, delete, exclude, find,
                              host, insertMany, master, project, rest,
                              select, sort, (=:))

  main :: IO ()
  main = do
      wetterDaten <- getWetterDaten
      pipe        <- connect (host "127.0.0.1")
      e           <- access pipe master "wetter2017" run
      close pipe
      print e

  --run :: Action IO ()
  run  = do
      clearData
      insertData 
      return ()

  clearData :: Action IO ()
  clearData = delete (select [] "daten")

  --insertData :: Action IO [Value]
  insertData   = do 
    **insertMany "daten" testDaten**

  insertData2 :: Action IO [Value]
  insertData2  = do 
    **insertMany "daten" getWetterDaten**

  testDaten = [["data" =: "Zeile0"],["data" =: "Zeile1"],
               ["data" =: "Zeile2"],["data" =: "Zeile3"]]

  getWetterDaten =  do
    fileContents <- fmap lines $ readFile "data.txt"
    let wetterDaten  = map makeMongo fileContents
    return wetterDaten

  makeMongo x = [ "data"      =: x]

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,它将数据作为参数传递给insertData2:

  {-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE ExtendedDefaultRules #-}
  module TestModule2 where

  import Database.MongoDB    (Action, Document, Document, Value, access,
                              close, connect, delete, exclude, find,
                              host, insertMany, master, project, rest,
                              select, sort, (=:))

  main :: IO ()
  main = do
      wetterDaten <- getWetterDaten
      processMongo wetterDaten
      return ()

  processMongo :: [Document] -> IO ()    
  processMongo wetterDaten = do
      pipe        <- connect (host "127.0.0.1")
      e           <- access pipe master "wetter2017" (run wetterDaten)
      close pipe
      print e

  run :: [Document] -> Action IO ()
  run wetterDaten = do
      clearData
      insertData 
      clearData2
      insertData2 wetterDaten
      return ()

  clearData :: Action IO ()
  clearData = delete (select [] "daten")

  clearData2 :: Action IO ()
  clearData2 = delete (select [] "daten2")

  insertData :: Action IO [Value]
  insertData   = do 
    insertMany "daten" testDaten

  insertData2 :: [Document] -> Action IO [Value]
  insertData2 wetterDaten = do 
    insertMany "daten2" wetterDaten

  testDaten :: [Document]
  testDaten = [["data" =: "Zeile0"],["data" =: "Zeile1"],
               ["data" =: "Zeile2"],["data" =: "Zeile3"]]

  getWetterDaten :: IO [Document]
  getWetterDaten =  do
    fileContents <- fmap lines $ readFile "data.txt"
    let wetterDaten  = map makeMongo fileContents
    return wetterDaten

  makeMongo :: String -> Document
  makeMongo x = [ "data"      =: x]