Skip to content

Support nested labels in 'persistent-mongoDB'#82

Closed
superbobry wants to merge 1 commit intoyesodweb:masterfrom
superbobry:feature/nested-mongodb-labels
Closed

Support nested labels in 'persistent-mongoDB'#82
superbobry wants to merge 1 commit intoyesodweb:masterfrom
superbobry:feature/nested-mongodb-labels

Conversation

@superbobry
Copy link
Copy Markdown

Hello, the current version of persistent-mongoDB doesn't support nested labels, so for example the following code doesn't work as expected, treating self.name as a flat label:

share [mkPersist sqlSettings] [persist|
Person sql=person
    name String Eq sql=self.name
    age Int
    deriving Show
|]

doSomething = do
    _personId <- insert $ Person "Garry" 3
    person <- selectFirst [PersonAge ==. 3] []
    liftIO $ print person

main :: IO ()
main = do
    withMongoDBConn "test" "127.0.0.1" $ \pool -> do
        runMongoDBConn DB.master action pool
        return ()

I've updated insertFields and mapFromDoc to support nested labels, so now self.name is saved as:

{self: {name: "Garry"}}

@gregwebs
Copy link
Copy Markdown
Member

Thanks for getting involved in improving MongoDB.

From your example I can't tell what the point of this is: you would be better off just using a single field called "name". Can you give a use case that better demonstrates the purpose?

@superbobry
Copy link
Copy Markdown
Author

Well, in my case, the database "schema" was already fixed by another tool we use, so I couldn't just go with "name". In general, I don't think this pattern is very common, but anyway, here's an example from MongoDB manual:

item : {
    "title" : <title> ,
    "price" : <price> ,
    "sku"   : <sku>   ,
    "features" : {
        "optical zoom" : <value> ,
            ...
    }
}

@gregwebs
Copy link
Copy Markdown
Member

yeah so technically speaking one only needs nested arrays, but a nested hash can be useful for organization. The way one is supposed to achieve nesting before this patch is through data types:

share [mkPersist sqlSettings] [persist|
Self
    name String Eq

Person sql=person
    self Self
    age Int
    deriving Show
|]

The only problem I see here is that Persistent does not really support embedded queries. It would be great to run a query like this:

    insert $ Person (Self "Garry") 3
    person <- selectFirst [PersonSelfName == "Bob"] []

@gregwebs
Copy link
Copy Markdown
Member

gregwebs commented Jul 8, 2012

You can do this query now:

    insert $ Person (Self "Garry") 3
    person <- selectFirst [PersonSelf ==. (Self "Bob")] []

Note this is a query about every value of the Self object, still no way to query on an embedded field.

My understanding of your patch is that it is for inserts and doesn't add query capabilities.
Are you ok with using the extra Self constructor instead of adding this patch?

@superbobry
Copy link
Copy Markdown
Author

Well, some of the document we have to work with are deeply nested, and having 3-4 intermediate objects probably won't work well. Anyway, this is clearly and exception, so I think it would be better to keep the patch local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants