DB Dynamic MongoDB

2

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

    
asked by anonymous 19.06.2014 / 14:49

2 answers

1

MongoDB is a document-oriented database, so it is NoSQL. This means no it is necessary to have a rigid structure for each record, you can adjust / modify it in the format you want.

MongoDB organizes these documents into collections to make it easier to query and organize information.

What does all this mean?

In my view, you do not need multiple banks, but several different document formats (records) - and you have chosen the right bank for this:)

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: CustomerScreen

// 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 has a different set of information; information pertinent to each client. The only common information between documents is the _id and owner fields, so you can find the information you need.

The great trick

All documents will have owner:'id do usuário dono' information. These fields will allow you to identify that documento X pertence ao usuário Y . So you can keep 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 kind of behavior when building a system.

    
19.06.2014 / 17:46
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 your implementation is that there would have to be an additional database to register your users. The rest of the implementation would depend on the technology chosen for this management.

    
19.06.2014 / 16:41