【MongoDB】Java驅動讀寫陣列
阿新 • • 發佈:2018-12-31
經常不用就忘了如何操作,記錄一筆。
寫
MongoClient client = new MongoClient(host, port);
MongoDatabase db = client.getDatabase(testdb);
MongoCollection<Document> col = db.getCollection(testcol);
String[] color = {"red", "blue", "green"};
Document doc = new Document("color", Arrays.asList(color));
col.insertOne(doc);
讀
Document query = new Document("color", "red");
MongoCursor<Document> c = col.find(query).iterator();
while (c.hasNext()) {
Document sub = c.next();
System.out.println(sub.get("color"));
System.out.println(sub);
}