-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMongoDatabase.java
More file actions
78 lines (69 loc) · 3.11 KB
/
MongoDatabase.java
File metadata and controls
78 lines (69 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package wtf.nucker.spigotutilities.utils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.sun.istack.internal.NotNull;
import org.bson.Document;
import org.bson.conversions.Bson;
/**
* A class for easily connecting to and using MongoDB
* @author Nucker
*/
public class MongoDatabase {
private final MongoClient client;
/**
* Connect to your database
* @param uri The full uri to connect to.
* @see MongoDatabase#buildURI(String, int) MongoDatabase#buildURI(String, int) for building a uri without authentication by passing the params
* @see MongoDatabase#buildURI(String, int, String, String) MongoDatabase#buildURI(String, int, String, String) for building a uri with authentication by passing params
*/
public MongoDatabase(String uri) {
this.client = new MongoClient(new MongoClientURI(uri));
}
/**
* Used to easily update data in a collection
* @param collectionName The collection you want to save to
* @param filter The filter used to find the document you want to update
* @param key The key of the data you want to update
* @param value The updated value
*/
public void save(String collectionName, Bson filter, @NotNull String key, Object value) {
getCollection(collectionName).replaceOne(filter, getCollection(collectionName).find(filter).first().append(key, value));
}
/**
* Easily collection from your database
* @param name The name of the collection
* @return An instance of the collection
*/
public MongoCollection<Document> getCollection(String name) {
return client.getDatabase("YourPluginName").getCollection(name);
}
/**
* Get the instance of the client
* @return The instance of the client
*/
public MongoClient getClient() {
return client;
}
/**
* Used for building uris by just passing params <b>This version does not support authentication</b>
* @param host The host of the database
* @param port The port the database is on
* @return The string of the uri with the params provided
*/
public static String buildURI(String host, int port) {
return "mongodb://[host]:[port]".replace("[host]", host).replace("[port]", String.valueOf(port));
}
/**
* Used for building uris by just passing params <b>This version does not support authentication</b>
* @param host The host of the database
* @param port The port the database is on
* @param username The username of the user your logging into
* @param password The password of the user your logging into
* @return The string of the uri with the params provided
*/
public static String buildURI(String host, int port, String username, String password) {
return "mongodb://[username]:[password]@[host]:[port]/?authSource=admin".replace("[username]", "[password]").replace("[port]",
String.valueOf(port).replace("[username]", username).replace("[password]", password));
}
}