parker.sikand
parker.sikand

Reputation: 1381

Haskell MongoDB, can't fetch _id field

Messing about with Yesod and MongoDB. I'm using the MongoDB library directly, not using the Persistent library.

I took this simple Yesod file server and converted it to use MongoDB. I am trying to get the _id field of my documents, but instead I am getting Nothing.

My documents have 4 fields: "_id", "filename", "mime", and "content". I am able to use the !? operator to get any field except _id, which returns Nothing.

If doc is one of my documents, and I do $(logDebug) $ show doc, I get my document printed to the console and I can see that all 4 fields are set, including the _id field.

If I do $(logDebug) $ show $ doc !? "_id" I get Nothing.

If I do $(logDebug) $ show $ head doc I get the _id field like _id: 12345

$(logDebug) $ show $ doc !? "filename" will give me the name, like file.txt

I don't see anything in the docs about _id being treated special, so whats up with this?

Upvotes: 0

Views: 146

Answers (1)

Fedor Gogolev
Fedor Gogolev

Reputation: 10551

Looks like you trying to use this code in ghci. Because it's not compileable without ghci extended defaulting rules. !? use typeclass Val and show use Show, so doc !? "_id" returns Val a => Maybe a And show requires Show a => a.

You should declare expecting type of !?, for example: $(logDebug) $ show $ ((doc !? "_id") :: Maybe ObjectId).

Upvotes: 2

Related Questions