Lesson 7. Creating a user database with MongoDB
Hey! As you're reading this, you know that I returned from Italy. It was very nice, but OK - you want to create a user database for your bot. Disputes about what DB is better has been around for a long time, but I will choose MongoDB. It is a high-performance, schema-free document-oriented database. Let's create actual 'body' of our bot. Well, as always. I will skip this step as I know you have your own bot and there is no need to pollute the great lesson unnecessary amount of code. If not, you can find all sources at GitHub repo. Now, import MongoDB's driver for Java. You can download it here or import it from Maven. With IntelliJ IDEA, it is easier than you expect. Go to File < Project Structure... < Libraries < + < From Maven and search for org.mongodb:mongo-java-driver
. That's all. Import it in your bot file:
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.json.JSONObject;
You will also need org.slf4j:slf4j-nop
library to disable additional logging, like this:
11:01:15.406 [pool-1-thread-1] DEBUG org.mongodb.driver.protocol.query - Query completed
11:01:25.174 [cluster-ClusterId{value='554dbecb1b554f11e86c3a69', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
Install it from Maven as you did with MongoDB Java Driver. Let's write our "check" function that will check if a user exists in our database. If not, write it.
private String check(String first_name, String last_name, int user_id, String username) {
MongoClientURI connectionString = new MongoClientURI("mongodb://host:port");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("db_name");
MongoCollection<Document> collection = database.getCollection("users");
long found = collection.count(Document.parse("{id : " + Integer.toString(user_id) + "}"));
if (found == 0) {
Document doc = new Document("first_name", first_name)
.append("last_name", last_name)
.append("id", user_id)
.append("username", username);
collection.insertOne(doc);
mongoClient.close();
System.out.println("User not exists in database. Written.");
return "no_exists";
} else {
System.out.println("User exists in database.");
mongoClient.close();
return "exists";
}
}
Don't do kernel panic, I will explain everything now.
Here we set a new connection to MongoDB's server:
MongoClientURI connectionString = new MongoClientURI("mongodb://host:port");
Replace host:port
with your Mongo's host and port. You can find how to set up MongoDB server for Ubuntu here. Then we set our database and collection. Replace this names with your own.
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("db_name");
MongoCollection<Document> collection = database.getCollection("users");
We search users like this:
long found = collection.count(Document.parse("{id : " + Integer.toString(user_id) + "}"));
And just check if user exists or not.
if (found == 0) {
Document doc = new Document("first_name", first_name)
.append("last_name", last_name)
.append("id", user_id)
.append("username", username);
collection.insertOne(doc);
mongoClient.close();
System.out.println("User not exists in database. Written.");
return "no_exists";
} else {
System.out.println("User exists in database.");
mongoClient.close();
return "exists";
}
So now our full function looks like this:
private String check(String first_name, String last_name, int user_id, String username) {
// Set loggers
java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(Level.OFF);
MongoClientURI connectionString = new MongoClientURI("mongodb://host:port");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("db_name");
MongoCollection<Document> collection = database.getCollection("users");
long found = collection.count(Document.parse("{id : " + Integer.toString(user_id) + "}"));
if (found == 0) {
Document doc = new Document("first_name", first_name)
.append("last_name", last_name)
.append("id", user_id)
.append("username", username);
collection.insertOne(doc);
mongoClient.close();
System.out.println("User not exists in database. Written.");
return "no_exists";
} else {
System.out.println("User exists in database.");
mongoClient.close();
return "exists";
}
}
Just check if a user exists in our database by calling this function when the user sends /start
:
import static java.lang.Math.toIntExact;
// Set variables
String user_first_name = update.getMessage().getChat().getFirstName();
String user_last_name = update.getMessage().getChat().getLastName();
String user_username = update.getMessage().getChat().getUserName();
long user_id = update.getMessage().getChat().getId();
String message_text = update.getMessage().getText();
long chat_id = update.getMessage().getChatId();
try {
telegramClient.sendMessage(message);
check(user_first_name, user_last_name, toIntExact(user_id), user_username);
} catch (TelegramApiException e) {
e.printStackTrace();
}
Well, that's all for now. Hope to see you soon!
Last modified: 23 November 2024