DB Dinamico Mongodb

Asked

Viewed 186 times

2

I’m starting a web project and I’m thinking about using java with mongoDB and Spring data. In this web project, each user would have their own database. That is, when the user logs in (this yes would be a shared database, where it would contain only the login data), I would like you to use the database of a certain user. Is that possible? What would be the most elegant way to implement ? I followed the example of this link, where uses spring data with mongodb.

2 answers

1

mongoDB is a document-driven database, so it is Nosql. It means that nay it is necessary to have a rigid structure for each record, you can adjust it/modify it in the format you want.

mongoDB organizes this document into collections to facilitate consultation and organization of information.

What all this means?

In my view, you don’t need multiple banks, but several different document formats (records) - and you chose the right bank for that :)

Example

Suppose you have a different client screen for each type of user (the screen is the same, but the information is different). How this should be organized:

Collection: Teladecustomer

// Documento para o cliente A:
{
 _id: 'UM OBJECTID PARA O CLIENTE A',
 owner: 'usuário2',
 nome: 'Cliente A',
 ultimoContato: '2014-01-01'
}

// Documento para o Cliente B:
{
 _id: 'UM OBJECTID PARA O CLIENTE B',
 owner: 'usuário1',
 razaoSocial: 'Cliente B ltda',
 totalVendas: 100,
 nomeContatoComercial: 'João'
}

Both documents (json), are in the same collection, however, each one has a different set of information; information pertaining to each client. The only information common among documents are the fields _id and owner, so that you can find the information you need.

The big trick

All documents will have the information owner:'id do usuário dono'. These fields will serve for you to identify that the documento X pertence ao usuário Y. Thus, you can maintain a bank in a standard (predictable) format, however, with different information for each user.

Multiple databases will be bad

If you install (create a new) database for each user, you will have a system for each user.

10 usuário = 10 sistemas diferentes

Maintenance for this type of system is extremely expensive and expensive. Avoid this type of behavior when building system.

0

In this link you passed, you would have to change this class to get the desired result:

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

    public @Bean
    MongoDbFactory mongoDbFactory() throws Exception 
    {
        // Ao invés de retornar uma Factory com "yourdb", retornaria uma factory por usuário
        // return new SimpleMongoDbFactory(new MongoClient(), "yourdb");
    }

    public @Bean
    MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

        return mongoTemplate;
    }
}

The problem with its implementation is that there would have to be a database additional to register your users. The rest of the implementation would depend on the technology chosen for this management.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.