2 回答

TA貢獻1851條經驗 獲得超5個贊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class MongoDBJDBC { public static void main(String[] args) { try { // 實例化Mongo對象,連接27017端口 Mongo mongo = new Mongo("localhost", 27017); // 連接名為yourdb的數據庫,假如數據庫不存在的話,mongodb會自動建立 DB db = mongo.getDB("test"); // Get collection from MongoDB, database named "yourDB" // 從Mongodb中獲得名為yourColleection的數據集合,如果該數據集合不存在,Mongodb會為其新建立 DBCollection collection = db.getCollection("test1"); // 使用BasicDBObject對象創建一個mongodb的document,并給予賦值。 BasicDBObject document = new BasicDBObject(); //document.put("id", 1001); //document.put("msg", "hello world mongoDB in Java"); // 將新建立的document保存到collection中去 //collection.insert(document); // 創建要查詢的document BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("name", "chen"); // 使用collection的find方法查找document DBCursor cursor = collection.find(searchQuery); // 循環輸出結果 while (cursor.hasNext()) { System.out.println(cursor.next()); } System.out.println("Hello World"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } } |
- 2 回答
- 0 關注
- 724 瀏覽
添加回答
舉報