Is there a way to set the collection dynamically when saving/getting a domain object in mongodb? I want to restrict users to a certain collection every time they do get/save since it is going to be a multi tenant db and each customer will have their own collection (I'm not using hibernate so multitenant-plugin is not an option)
My thoughts are to setup a service that identifies the correct collection and that is session based (the collection name will depend on url resolving amongst other things)
I thought of overriding get/set somehow for all domain classes with doWithDynamicMethods in a plugin. But I need to be able to set the collection upon 'save' and 'get'.
So I know you can specify a collection with the database mongo object, but could you just pass the 'collection' name whan invoking 'save' .e.g. Person p = new Person(name:john).save(collection:collectionNameService.getCollectionName(Person),flush:true)
In this case I could just override the generic save/get for domain objects and call the save with the extra 'collection' 开发者_开发知识库parameter instead.
Suggestions welcome :)
Cordially, Per
I've done something very similar, but with some PHP libraries.
But yes it definitely makes sense to modify the "save" and "load" methods to load from the correct collection. MongoDB will automatically create the collection for you, so at a basic level this will work.
Only two major caveats:
- Are you adding indexes to those new collections? You don't want to "ensureIndex" on every single action, but you'll probably want some way to know that a new collection has been created so that you can index (likely a job on the server)
- How many users do you plan on having? You may want to look at some limitations about the number of collections available.
精彩评论