MongoDB 項目整合 mongo-driver 3.4.2

來源:互聯網
上載者:User

標籤:close   bpa   for   專業   rac   read   too   return   建立   

第一次寫技術!大白話講講。拿著用就可以了

 

本人是,NET的技術人員,會點JAVA所以很多不專業,見諒哈

 

剛剛開始使用mongo 整整搞了兩天我才搞個半桶水,還是將就著用吧

 

隨便把mongo在win的搭建也寫下。其實這個沒啥寫的。很簡單

 

我使用的是mongo最新版本的

安裝的目錄是C盤

然後資料庫放在其他盤下面

根據下面的命令建立下目錄就可以了。MongoDB.log這個是不用建立的

建立mongo服務。這樣開機就可以使用了

 

[plain] view plain copy 
  1. mongod --dbpath O:\mongodb\data\db --logpath O:\mongodb\data\log\MongoDB.log --install --serviceName MongoDBMet  


這樣在服務你們就會出現我們建立的伺服器了。有點不知道怎麼回事,我的服務名字居然不是我自己寫的,一直是MongoDB

 

 

現在的庫基本是建立起來的。特別想建個叢集。但是不知道咋建。還在研究中

管理工具的使用是個頭痛的問題,網上我找了兩個,好像不支援最新的mongo庫,不知道是不是配置的原因還是怎麼了

最好沒法使用了收費的管理工具“NoSQL Manager for MongoDB” 這個用起來很強大,使用也很方便,能夠相容最新的庫

 

 

 

現在開始做JAVA連結  這個地方有個坑,可能對我來說是個坑。就是使用mongodb-driver-3.4.2.jar 這個包必須要結合這其他兩個包使用。分別是mongodb-driver-core-3.4.2.jar和bson-3.4.2.jar,剛剛開始不知道,,就引用了第一個。編寫都沒報錯。運行就報錯了。連結不上mongodb,當時看了好多菜搞明白,還有就是這個三個包一定要版本一致,不然也是報錯的。我使用的是最新的3.4.2.都是最新的。現在使用正常

 

現在貼代碼

 

[java] view plain copy 
  1.        MongoClient mongoClient = null;  
  2. MongoDatabase database = null;  
  3. MongoCollection<Document> collection = null;  
  4.   
  5. public MetstrFY() {  
  6.     String MongoHOST = SetSystemProperty.readValue("MongoHOST");//地址  
  7.     String MongoDK = SetSystemProperty.readValue("MongoDK");//連接埠  
  8.     String MongoFYdb = SetSystemProperty.readValue("MongoFYdb");//資料庫名稱  
  9.     String MongoFYdata = SetSystemProperty.readValue("MongoFYdata");//表名稱  
  10.   
  11.     mongoClient = new MongoClient(MongoHOST, Integer.parseInt(MongoDK));  
  12.     database = mongoClient.getDatabase(MongoFYdb);  
  13.     collection = database.getCollection(MongoFYdata);  
  14. }  

傳ID查詢資訊出來。返回的是JSON格式哦。自己轉換吧,

[java] view plain copy 
  1.       public String GetModel(String Pmid) {  
  2.   
  3.     MetFYModel model = new MetFYModel();  
  4.     Document myDoc = collection.find(new BasicDBObject("id", id)).first();  
  5.     mongoClient.close();  
  6.     return myDoc.toJson();  
  7.     // return model;  
  8.   
  9. }  

好了。剩下的路還很多。我得慢慢研究啊,

 

使用的包發給大家,可以試試發哈

 

https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-core/3.4.2/mongodb-driver-core-3.4.2.jar

 

https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.4.2/

 

https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.4.2/

 

MongoDB 項目整合 mongo-driver 3.4.2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.