#MongoDB implementation

1 messages · Page 1 of 1 (latest)

hybrid stratus
#

@cerulean rapids here will work

cerulean rapids
#

perfec

#

To start do You have any experience with mongo

hybrid stratus
#

Yeah ive made applications with mongo before just never with java

cerulean rapids
#

right perfect

#

so in Java You use a module called mongo-java-driver in case using Maven

hybrid stratus
#

Yeah I have all the drivers installed and stuff

#

Using gradle btw

cerulean rapids
#

good

#

let starts with the basic

#

I'm My case i always use mongo with uris for commection

hybrid stratus
#

Yeah I have it setup and connected, I just need help with like data part

cerulean rapids
#

Oh okay, sorry i Will explain that

hybrid stratus
#
package me.tls.rpg.common.data;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import me.tls.rpg.common.data.player.PlayerData;
import org.bukkit.plugin.java.JavaPlugin;

public class DatabaseManager {

    private final MongoDatabase database;

    private final MongoCollection<PlayerData> playerDataCollection;

    public DatabaseManager(JavaPlugin plugin, String URI, String database, String collection) {
        try (MongoClient client = MongoClients.create(URI)) {
            plugin.getLogger().info("=> Connection to Database Successful");
            this.database = client.getDatabase(database);
            this.playerDataCollection = this.database.getCollection(collection, PlayerData.class);
        }
    }

    public MongoDatabase getDatabase() {
        return this.database;
    }

    public MongoCollection<PlayerData> getPlayerDataCollection() {
        return this.playerDataCollection;
    }

}

this is my DatabaseManager right now, when I did previous work with mongo it was in go so there was no OOP so i think this approach is very bad in java but I dont know

cerulean rapids
#

i mean OOP is the Best way

#

I can't work without OOP

hybrid stratus
#

Yes ofc for a document orianted database

cerulean rapids
#

Your code looks fine

#

do You have any issue with it?

hybrid stratus
#

Not that I know of

#

I just dont know how to layout my data- and how to efficently update, insert, remove ect as needed

#

I have an example json if you want me to send that?

cerulean rapids
#

well thats part of designing the db

#

and also how You work with the db

hybrid stratus
#

Yeah, I have the thing mapped out and how I think it would best work. I just dont know the best way to implement it as its a huge data structure

cerulean rapids
#

a) As usual when You work with databases You should always use them async

b) Can also implement catching systems, like Guava cache for example, Caffeine or Even use a HashMap / List

hybrid stratus
#

ugh i hate async

cerulean rapids
#

oh well Big issue hajaja

hybrid stratus
#

yeah lol

cerulean rapids
#

You Will have yo love it

#

because is our Best friend

#

specially when doing working with things that has connections to remote things

hybrid stratus
#

here is the example data layout

#

there will be more added but

cerulean rapids
#

okay

#

In My case i take approch of creating a Collection for a single reposability

#

where users Collection just store users info, not his punish

hybrid stratus
#

Yeah thats fair

cerulean rapids
#

I store punish un another Collection

hybrid stratus
#

Yeah I would probably do that

cerulean rapids
#

and in user Collection link each punish object ID to an array of punish ids

#

thats is most applied if You worked before with relational dbs like MySQL for example

hybrid stratus
#

Yeah I have

#

those get super confusing though with asmuch data as you need for an RPG

cerulean rapids
#

yeah that why You should first design the diagram of the db. And then start coding

#

I use lucid chart for designing the dbs models

hybrid stratus
cerulean rapids
#

The ones i know most of them uses bad practices in general. I wouldnt like to recommend something which has bad practices