溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          添加Mongodb驅動到你的classpath環境中
          package com.mongodb;


          public class Mongodb {
              
              
          private Mongo mongo;
              
          private DBCollection dbCollection;
              
          private DB db;
              
              
          public Mongodb() throws Exception{
                  
          //使用ip地址創建Mongo對象
                  mongo=new Mongo("127.0.0.1");
                  
          //獲取orcl數據庫
                  db=mongo.getDB("orcl");
                  
          //判斷是否存在集合person
                  boolean b=db.collectionExists("person");
                  System.out.println(
          "是否存在集合[person]:"+b);
                  dbCollection 
          = db.getCollection("person");
                  
          long count=dbCollection.count();
                  System.out.println(
          "總記錄數是:"+count);
                  DBCursor cursor 
          =dbCollection.find().skip(20).limit(20);
                  
          while(cursor.hasNext()){
                      System.out.println(cursor.next());
                  }
                  
                  
              }
              
              
              
              
          public void list(){
                  dbCollection
          =db.getCollection("person");
                  BasicDBObject dbObject
          =new BasicDBObject();
                  dbObject.put(
          "age"new BasicDBObject("$gt",20).append("$lt"60));
                  DBCursor cursor 
          = dbCollection.find(dbObject);
                  System.out.println(cursor.count());
                  
          while(cursor.hasNext()){
                      System.out.println(cursor.next());
                  }
                  
              }
              
              
              
              
          private boolean insert(){
                  
                  dbCollection
          =db.getCollection("person");
                  BasicDBObject dbObject
          =new BasicDBObject();
                  dbObject.put(
          "name""zhangsan");
                  dbObject.put(
          "age"20);
                  WriteResult writeResult 
          = dbCollection.save(dbObject);
                  System.out.println(writeResult.getN());
                  
          return false;
              }
              
              
          private boolean delete(){
                  dbCollection
          =db.getCollection("person");
                  BasicDBObject dbObject
          =new BasicDBObject();
                  dbObject.put(
          "name""zhangsan");
                  WriteResult writeResult 
          = dbCollection.remove(dbObject);
                  System.out.println(writeResult.getN());
                  
          return false;
              }
              
              
              
          private boolean update(){
                  dbCollection
          =db.getCollection("person");
                  BasicDBObject dbObject
          =new BasicDBObject();
                  dbObject.put(
          "name""s0020");
                  BasicDBObject dbObject2
          =new BasicDBObject();
                  dbObject2.put(
          "name""s0020");
                  dbObject2.put(
          "age"65);
                  WriteResult writeResult 
          = dbCollection.update(dbCollection.findOne(dbObject), dbObject2);
                  System.out.println(writeResult.getN());
                  
          return false;
              }
              
              
              
          private Object getOne(){
                  dbCollection
          =db.getCollection("person");
                  BasicDBObject dbObject
          =new BasicDBObject();
                  dbObject.put(
          "name""s0020");
                  
          //dbObject.put("age", 65);
                  DBObject object=dbCollection.findOne(dbObject);
                  System.out.println(object.toMap().get(
          "name")+"\t"+object.toMap().get("age"));
                  
          return object;
              }
              
              
              
          public static void main(String[] args) throws Exception{
                  Mongodb mongodb
          =new Mongodb();
                  
          //mongodb.insert();
                  
          //mongodb.getOne();
                  
          //mongodb.update();
                  
          //mongodb.delete();
                  
          //mongodb.list();
              }
          }
          posted on 2012-03-12 16:05 雪山飛鵠 閱讀(5813) 評論(2)  編輯  收藏 所屬分類: MongoDB

          Feedback

          # re: Java操作Mongodb 2013-05-27 11:07 安安
          不錯  回復  更多評論
            

          # re: Java操作Mongodb 2014-07-03 14:34 zuidaima
          不錯  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 高平市| 铁力市| 保靖县| 遵化市| 新疆| 上犹县| 乐亭县| 娄底市| 沧源| 如东县| 黔东| 密山市| 江安县| 淮阳县| 通海县| 梁河县| 元氏县| 民和| 泊头市| 吴桥县| 大兴区| 西吉县| 阿图什市| 固安县| 开阳县| 大冶市| 宜兴市| 麟游县| 郁南县| 北安市| 乌兰县| 巴马| 临澧县| 会宁县| 唐山市| 禹州市| 武威市| 横山县| 涞源县| 宜兴市| 玉山县|