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()); } } }
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()); } } }
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<Document> 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()); } } }
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<Document> 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()); } } }
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<Document> collection = database.getCollection("employees"); FindIterable<Document> 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); } } }