java 使用mongodb

來源:互聯網
上載者:User

標籤:技術   std   database   ocr   iterable   handle   while   tor   nts   

1.先串連你的mongodb

看串連是否有問題,代碼

public class MongoDB2 {private static MongoDatabase mongoDatabase = null;private static int port = 27017;private static String userName = "XX";private static String password="XX" ;private static String database = "gatp";private static String host="XXX";/** * mongo db 串連 *  *  */public  void mongoConnect() {try {// host和port進行轉換encryptionDecryption decryption = new encryptionDecryption();ServerAddress serverAddress = new ServerAddress(host, port);List<ServerAddress> addresses = new ArrayList<ServerAddress>();addresses.add(serverAddress);MongoCredential credential = MongoCredential.createCredential(userName,database, password.toCharArray());List<MongoCredential> credentials = new ArrayList<MongoCredential>();credentials.add(credential);// 通過串連認證擷取MongoDB串連MongoClient mongoClient = new MongoClient(addresses, credentials);mongoDatabase = mongoClient.getDatabase(database);Log.logInfo(mongoDatabase);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}public static void main(String[] args) {MongoDB2 db=new MongoDB2();db.mongoConnect(); //確認串連正確}

  串連成功後會顯示mogodb的id,錯誤會顯示認證失敗

串連失敗的案例

成功會顯示

 

2.對mogodb進行資料的插入

封裝的方法insertCollection,插入可數字,字串,

public boolean insertCollection(String collectionName, List<Document> documents) {boolean insertResult = false;try {MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);collection.insertMany(documents);insertResult = true;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return insertResult;}public static void main(String[] args) {MongoDB2 db=new MongoDB2();db.mongoConnect(); //確認串連正確Date day=new Date();    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//產生json字串JSONObject json = new JSONObject();json.put("id","1");json.put("name","張三");json.put("pwd","123456");System.out.println(json);Document testDocument = new Document();testDocument.put("times", df.format(day));//插入時間testDocument.put("name","zhangjun" ); //插入名稱testDocument.put("info", json.toString()); //插入json字串List<Document> documents = new ArrayList<Document>();documents.add(testDocument);db.insertCollection("test_log_info", documents);}

  

 

3.查詢資料

/** * 擷取集合 *  * @param collectionName *            集合名 * @param testDocument *            條件 , 支援多對條件 * @return *  */public MongoCursor<Document> getCollection(String collectionName, Document testDocument) {MongoCursor<Document> mongoCursor = null;try {MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);FindIterable<Document> resultDocument = collection.find(testDocument);mongoCursor = resultDocument.iterator();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return mongoCursor;}public static void main(String[] args) {MongoDB2 db=new MongoDB2();db.mongoConnect(); //確認串連正確Document testDocument = new Document();testDocument.put("name", "zhangjun");MongoCursor<Document> resultDocument = db.getCollection("test_log_info", testDocument);while(resultDocument.hasNext()){  System.out.println(resultDocument.next());//擷取所有System.out.println(resultDocument.next().get("_id")); //擷取某個值          } }

 

java 使用mongodb

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.