[[oktatas:programozas:java:java adatbázis:mongodb|< MongoDB]]
====== MongoDB kezdés ======
* **Szerző:** Sallai András
* Copyright (c) 2023, Sallai András
* Szerkesztve: 2024
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== Adatbázisok =====
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
client.listDatabaseNames().forEach(System.out::println);
Teljes kód:
import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
public class App {
public static void main(String[] args) throws Exception {
try {
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
client.listDatabaseNames().forEach(System.out::println);
} catch (MongoException e) {
System.err.println("Hiba! A kapcsolódás sikertelen!");
System.err.println(e.getMessage());
}
}
}
===== Gyűjtemények =====
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
MongoDatabase database = client.getDatabase("zoldzrt");
database.listCollectionNames().forEach(System.out::println);
Teljes kód:
import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
public class App {
public static void main(String[] args) throws Exception {
try {
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
MongoDatabase database = client.getDatabase("zoldzrt");
database.listCollectionNames().forEach(System.out::println);
} catch (MongoException e) {
System.err.println("Hiba! A kapcsolódás sikertelen!");
System.err.println(e.getMessage());
}
}
}
===== Beszúrás =====
Document doc = new Document();
doc.put("name", "Erős István");
doc.put("city", "Szeged");
doc.put("salary", 395.2);
collection.insertOne(doc);
Teljes kód:
import org.bson.Document;
import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class App {
public static void main(String[] args) throws Exception {
try {
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
MongoDatabase database = client.getDatabase("zoldzrt");
MongoCollection collection = database.getCollection("employees");
Document doc = new Document();
doc.put("name", "Lime Lajos");
doc.put("city", "Pécs");
doc.put("salary", 392.8);
collection.insertOne(doc);
} catch (MongoException e) {
System.err.println("Hiba! A kapcsolódás sikertelen!");
System.err.println(e.getMessage());
}
}
}
===== Első =====
import org.bson.Document;
import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class App {
public static void main(String[] args) throws Exception {
try {
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
MongoDatabase database = client.getDatabase("zoldzrt");
MongoCollection collection = database.getCollection("employees");
Document document = collection.find().first();
System.out.println(document.get("name"));
} catch (MongoException e) {
System.err.println("Hiba! A kapcsolódás sikertelen!");
System.err.println(e.getMessage());
}
}
}
===== Program =====
import org.bson.Document;
import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class App {
public static void main(String[] args) throws Exception {
try {
String url = "mongodb://root:titok@localhost:27017";
ConnectionString cs = new ConnectionString(url);
MongoClient client = MongoClients.create(cs);
MongoDatabase database = client.getDatabase("zoldzrt");
MongoCollection collection = database.getCollection("employees");
FindIterable docs = collection.find();
docs.forEach(doc -> System.out.println(doc));
} catch (MongoException e) {
System.err.println("Hiba! A kapcsolódás sikertelen!);
System.err.println(e.getMessage());
System.err.println(e);
}
}
}
===== Link =====
* https://www.baeldung.com/java-mongodb (2024)