MongoDB_Java連線mongo 使用Java程式更新集合中的文件
阿新 • • 發佈:2018-12-24
之前mongo資料庫內容
程式碼
之後import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; import com.mongodb.WriteResult; /** * 使用Java程式更新集合中的文件 */ public class JavaDocUpDate { public static void main(String[] args) { try { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); DBCollection dbCollection = db.getCollection("user"); BasicDBObject query = new BasicDBObject("name", "C++"); BasicDBObject update = new BasicDBObject("name","CCC"); WriteResult result = dbCollection.update(query, update); System.out.println(result.toString()); } catch (Exception e) { e.printStackTrace(); } } }