標籤:io ar os sp java for 資料 on art
package mongodb;
import java.net.UnknownHostException;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
public class MongoCRUD {
public static void main(String[] args) {
try {
這樣就建立了一個MongoDB的資料庫連接對象,它預設串連到當前機器的localhost地址,連接埠是27017。
Mongo mongo = new Mongo();
也可以這樣 串連資料庫
//MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//獲得所有資料庫名字
List<String> list = mongo.getDatabaseNames();
for (String dbName : list) {
System.out.println("DataBaseName="+dbName);
}
//得到資料庫 如果沒有,則建立
DB db = mongo.getDB("myTest");
//擷取db資料庫的所有表名(在這裡用集合表示)
Set<String> set = db.getCollectionNames();
for (String string : set) {
System.out.println("tablesName="+string);
}
//得到該資料庫的表 如果沒有,則建立
DBCollection coll = db.getCollection("myTables");
//第一種方式用BasicDBObject插入JSON格式資料
BasicDBObject dbObject1 = new BasicDBObject();
dbObject1.append("id", 1).append("name", "jerry").append("age", 23).append("job", "程式員").append("address", "深圳");
BasicDBObject dbObject2 = new BasicDBObject();
dbObject2.put("id", 2);
dbObject2.put("name", "enen");
dbObject2.put("age", 22);
dbObject2.put("address", "惠州");
dbObject2.put("jerry", dbObject1);
coll.insert(dbObject2);
//第二種用BasicDBObjectBuilder
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("database", "mkyongDB")
.add("table", "hosting");
BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
.add("records", "99")
.add("index", "vps_index1")
.add("active", "true");
documentBuilder.add("detail", documentBuilderDetail.get());
coll.insert(documentBuilder.get());
//第三種用Map對象
Map documentMap =new HashMap();
documentMap.put("database", "mkyongDB");
documentMap.put("table", "hosting");
Map documentMapDetail =new HashMap();
documentMapDetail.put("records", "99");
documentMapDetail.put("index", "vps_index1");
documentMapDetail.put("active", "true");
documentMap.put("detail", documentMapDetail);
coll.insert(new BasicDBObject(documentMap));
// 第四種方法,也就是最簡單的,即直接插入JSON格式資料
String json ="{‘database‘ : ‘mkyongDB‘,‘table‘ : ‘hosting‘,"+
"‘detail‘ : {‘records‘ : 99, ‘index‘ : ‘vps_index1‘, ‘active‘ : ‘true‘}}}";
DBObject dbObject =(DBObject)JSON.parse(json);
coll.insert(dbObject);
//查詢所有的資料
DBCursor cursor = coll.find();
System.out.println(cursor.count());
while(cursor.hasNext()){
System.out.println(cursor.next());
}
//刪除 找到第一個刪除
DBObject findOne = coll.findOne();
coll.remove(findOne);
System.out.println("修改前------------------------");
DBObject updateCondition=new BasicDBObject();
updateCondition.put("name", "enen");
DBObject updatedValue=new BasicDBObject();
updatedValue.put("age", 3);
updatedValue.put("address", "boss");
DBObject updateSetValue=new BasicDBObject("$set",updatedValue);
coll.update(updateCondition, updateSetValue,true,false);//後面兩個參數設定,前面一個是where條件不存在時,插入資料。後面一個是是否修改多條記錄。
System.out.println("修改後---------------");
DBCursor find = coll.find();
while(find.hasNext()){
DBObject next = find.next();
System.out.println(next);
}
System.out.println(JSON.serialize(cursor));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在這裡因為沒什麼時間,所有沒有抽取到方法,通過方法調用,擠在一個主類中,有時間在把他重構。
關於Mongdb的java的CRUD操作