1. 程式人生 > >【MongoDB】Java驅動讀寫陣列

【MongoDB】Java驅動讀寫陣列

經常不用就忘了如何操作,記錄一筆。

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);
}