Reputation: 10383
What is the correct way of retrieving object id as string? I am getting type errors and cant figure out how to cast it. Calling 'show' on it does not work.
Also, the other way round, if i have object id as string received from URL, for instance,how do I write it back to mongo. I don't understand how to convert it correctly.
Upvotes: 0
Views: 699
Reputation: 13876
I assume you are using mongoDB driver. ObjectId
has Show
and Read
instances. Example:
import Database.MongoDB
import Text.Read
main :: IO ()
main = do
oid <- genObjectId
putStrLn $ show oid
print (readEither (show oid) :: Either String ObjectId)
Output:
51d16cec08d0cf312a000000
Right 51d16cec08d0cf312a000000
Upvotes: 2