mongoDB用java實現增刪改查

來源:互聯網
上載者:User

標籤:blog   io   os   ar   使用   java   for   sp   資料   

package mongo;import java.net.UnknownHostException;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.Mongo;import com.mongodb.MongoException;public class connection {public static void main(String[] args){try{Mongo m = new Mongo("localhost", 27017);DB db = m.getDB("company");DBCollection collection = db.getCollection("employees");BasicDBObject employee = new BasicDBObject();employee.put("name", "Hannah");employee.put("no", 2);collection.insert(employee);BasicDBObject searchEmployee = new BasicDBObject();searchEmployee.put("no", 2);DBCursor cursor = collection.find(searchEmployee);while(cursor.hasNext()){System.out.println(cursor.next());}System.out.println("The Search Query has Executed!"); }catch(UnknownHostException e){e.printStackTrace();}catch(MongoException e){e.printStackTrace();}}}

 { "_id" : { "$oid" : "5454fa4458404d12637d418c"} , "name" : "Hannah" , "no" : 2}
The Search Query has Executed!

import java.net.UnknownHostException;  import java.util.Set;    import com.mongodb.BasicDBObject;  import com.mongodb.DB;  import com.mongodb.DBCollection;  import com.mongodb.DBCursor;  import com.mongodb.DBObject;  import com.mongodb.Mongo;  import com.mongodb.MongoException;    public class Test {        /**      * @author gaogao      * @param args      * @throws MongoException      * @throws UnknownHostException      */      public static void main(String[] args) throws UnknownHostException,              MongoException {          // TODO Auto-generated method stub          // 串連本機資料庫          Mongo m = new Mongo();          // 建立名為new_test_db的資料庫          DB db = m.getDB("new_test_db");          // 擷取new_test_db中的集合(類似於擷取關聯式資料庫中的表)          Set<String> cols = db.getCollectionNames();          // 列印出new_test_db中的集合,這裡應當為null          for (String s : cols) {              System.out.println(s);          }          // 建立一個叫做"new_test_col"的集合          DBCollection collection = db.getCollection("new_test_col");          // 初始化一個基本DB對象,最終插入資料庫的就是這個DB對象          BasicDBObject obj = new BasicDBObject();          // 放入幾個索引值對          obj.put("from", "搞搞");          obj.put("to", "寶寶");          obj.put("subject", "狗子愛寶子");          //插入對象          collection.insert(obj);          //查看一條記錄,findOne()=find().limit(1);          DBObject dbobj=collection.findOne();          //列印出剛才插入的資料          System.out.println(dbobj);          //現在我們來插入9條{ranking:i}的資料          for(int i=0;i<9;i++){              collection.insert(new BasicDBObject().append("ranking", i));          }          //列印集合中的資料總數,這裡應當輸出10          System.out.println(collection.getCount());          //下面我們來遍曆集合,find()方法返回的是一個遊標(cursor),這裡的概念和關聯式資料庫很相似          DBCursor cursor=collection.find();          //然後我們使用這個遊標來遍曆集合          while(cursor.hasNext()){              System.out.println(cursor.next());          }          //下面來看一些略複雜一點的查詢技巧,第一個,簡單的條件查詢,查詢ranking為1的記錄          BasicDBObject query=new BasicDBObject();          query.put("ranking", 1);          cursor=collection.find(query);          while(cursor.hasNext()){              System.out.println(cursor.next());          }          //下面是更複雜的條件查詢,查詢ranking大於5小於9的記錄          query=new BasicDBObject();          query.put("ranking", new BasicDBObject("$gt", 5).append("$lt", 9));          cursor=collection.find(query);          while(cursor.hasNext()){              System.out.println(cursor.next());          }          //最後刪除我們的測試資料庫          m.dropDatabase("new_test_db");      }  } 

 

mongoDB用java實現增刪改查

相關文章

聯繫我們

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