MongoDB--Getting Started with Java Driver,mongodbdriver
原文連結 http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/
介紹
本文的目的是讓你對如何使用Mongodb的java-driver驅動有一個簡單的瞭解,當你閱讀完本文你就可以參考其他文章以瞭解更多資訊。
下載java-driver驅動
你可以在這裡下載需要的驅動。
添加java-driver驅動將下載的mongo-java-driver-2.12.3.jar 拷貝到你的lib目錄下。
擷取資料庫物件
MongoClient mongoClient = new MongoClient( "localhost" , 5000 ); // 5000 為執行個體連接埠號碼,預設啟動連接埠號碼為27017
DB db = mongoClient.getDB("mydb"); // 必須要先建立資料庫
擷取集合名稱Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
擷取集合對象 DBCollection coll = db.getCollection("blog");
擷取集合文檔數
System.out.println(coll.getCount());
插入一個文檔 BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);
插入多個文檔
for (int i=0; i < 100; i++) {
coll.insert(new BasicDBObject("i", i));
}
使用遊標查詢滿足條件的文檔
BasicDBObject query = new BasicDBObject("i", 71);
DBCursor cursor = coll.find(query);
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
BulkWriteOperation builder = coll.initializeOrderedBulkOperation();
builder.insert(new BasicDBObject("_id", 1));
builder.insert(new BasicDBObject("_id", 2));
builder.insert(new BasicDBObject("_id", 3));
更新已存在的文檔 builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2)));
刪除已存在的文檔 builder.find(new BasicDBObject("_id", 2)).removeOne();
builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4));
BulkWriteResult result = builder.execute();
selenium-webdriver-java:可以用ff找到頁面的元素id,但是用findElement卻提示找不到這個元素
看下導覽列是否嵌在iframe裡面,如果是,先進入該iframe,然後在定位元素
Error while performing database login with the driver 到主機的TCP/IP串連失敗javanetunkownHost
---------------------------------------------------------------
從這個異常來判斷,是因為你的串連資料庫的 url 中的資料庫伺服器 Ip 地址(或者是主機名稱)寫的不對。請檢查。
如果還不能解決,請說明你用的是哪個資料庫,你的串連 url ,可以幫你進一步判斷出錯原因。
---------------------------------------------------------------