Slide 15
Slide 15 text
ドキュメントデータベース機能のサポート
X DevAPI を用いたデータ操作(Java の例)
15
// サーバに接続
String sessionString = "mysqlx://localhost:33060/testdb?user=testuser&password=P@55w0rd"
Session session = new SessionFactory().getSession(sessionString);
// DB に接続
Schema db = session.getSchema("testdb");
// コレクション 'testcollection' を作成
Collection collection = db.createCollection("testcollection", true);
// コレクションにドキュメントを追加
collection.add("{\"id\":1, \"name\":\"MySQL\", \"version\":\"5.7\"}").execute();
collection.add("{\"id\":2, \"name\":\"MySQL\", \"version\":\"8.0\"}").execute();
collection.add("{\"id\":3, \"name\":\"MariaDB\", \"version\":\"10.5\"}").execute();
// コレクションの「 id」列にインデックスを追加
collection.createIndex("id_index", "{\"fields\": [{\"field\": \"$.id\", \"type\": \"INT\"}]}");
// コレクションから「 version LIKE '%8.0%'」を検索
DocResult docs1 = collection.find("version like :ver").bind("ver", "%8.0%").execute();
// コレクションから「 id=2」を検索
DocResult docs2 = collection.find("id = :id").bind("id", 2).execute();