Java操作MongoDB

 java驱动程序是Mongodb中的驱动程序之一,也是比较成熟的Mongodb驱动程序之一,下面介绍了使用java连接、操作Mongodb。

          一、安装java驱动程序

                 Mongodb的java驱动程序是一个jar包,可以在:https://github.com/mongodb/mongo-java-driver/downloads 下载,下载的jar导入到eclipse的项目中即可。

          二、java操作Mongodb

                 java操作Mongodb常用的几个类:

                Mongo:连接服务器,执行一些数据库操作的选项,如新建立一个数据库等

                DB:对应一个数据库,可以用来建立集合等操作

                DBCollection:对应一个集合(类似表),可能是我们用得最多的,可以添加删除记录等

                DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。

                DBCursor:用来遍历取得的数据,实现了Iterable和Iterator

 

  1. private static void mongodbOperating(){ 
  2.         try { 
  3.             //有多种构造方法,选择一种(IP、port) 
  4.             Mongo m = new Mongo( "192.168.21.111" , 27017 ); 
  5.             //选择数据库,如果没有这个数据库的话,会自动建立 
  6.             DB  db = m.getDB( "mydb" ); 
  7.  
  8.             //建立一个集合,和数据库一样,如果没有,会自动建立 
  9.             DBCollection collection = db.getCollection("myCollectionTest"); 
  10.              
  11.             BasicDBObject doc = new BasicDBObject(); 
  12.             doc.put("name", "MongoDB"); 
  13.             doc.put("type", "database"); 
  14.             doc.put("count", 1); 
  15.  
  16.             BasicDBObject info = new BasicDBObject(); 
  17.             info.put("x", 203); 
  18.             info.put("y", 102); 
  19.             doc.put("info", info); 
  20.  
  21.             //插入一条数据,数据如下 
  22.             // { 
  23.             //     "name" : "MongoDB", 
  24.             //     "type" : "database", 
  25.             //     "count" : 1, 
  26.             //     "info" : { 
  27.             //                 x : 203, 
  28.             //                 y : 102 
  29.             //               } 
  30.             //  } 
  31.             // 可以循环插入多条数据 
  32.             collection.insert(doc); 
  33.             //查找第一条数据,显示如下,_id是系统自动帮加上的,全局唯一 
  34.             //{ "_id" : "49902cde5162504500b45c2c" , "name" : "MongoDB" , "type" : "database" , "count" : 1 , "info" : { "x" : 203 , "y" : 102}} 
  35.             DBObject myDoc = collection.findOne(); 
  36.             System.out.println(myDoc); 
  37.              
  38.             //插入多条数据 
  39.             for (int i=0; i < 100; i++) { 
  40.                 collection.insert(new BasicDBObject().append("i", i)); 
  41.             } 
  42.  
  43.              
  44.             //获取文档条数 
  45.             System.out.println(collection.getCount()); 
  46.              
  47.             //使用Cursor 获取所有文档 
  48.             DBCursor cursor = collection.find(); 
  49.             try { 
  50.                 while(cursor.hasNext()) { 
  51.                     System.out.println(cursor.next()); 
  52.                 } 
  53.             } finally { 
  54.                 cursor.close(); 
  55.             } 
  56.              
  57.             //查找操作,获取单条记录 
  58.             //{ "_id" : "49903677516250c1008d624e" , "i" : 71 } 
  59.             BasicDBObject query = new BasicDBObject(); 
  60.             query.put("i", 71); 
  61.             cursor = collection.find(query); 
  62.             try { 
  63.                 while(cursor.hasNext()) { 
  64.                     System.out.println(cursor.next()); 
  65.                 } 
  66.             } finally { 
  67.                 cursor.close(); 
  68.             } 
  69.  
  70.             //查找 i>50的项 
  71.             query = new BasicDBObject(); 
  72.             query.put("i", new BasicDBObject("$gt", 50));  // e.g. find all where i > 50 
  73.             cursor = collection.find(query); 
  74.             try { 
  75.                 while(cursor.hasNext()) { 
  76.                     System.out.println(cursor.next()); 
  77.                 } 
  78.             } finally { 
  79.                 cursor.close(); 
  80.             } 
  81.  
  82.  
  83.             //查找 20<i<=30 
  84.             query = new BasicDBObject(); 
  85.             query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));  // i.e.   20 < i <= 30 
  86.             cursor = collection.find(query); 
  87.             try { 
  88.                 while(cursor.hasNext()) { 
  89.                     System.out.println(cursor.next()); 
  90.                 } 
  91.             } finally { 
  92.                 cursor.close(); 
  93.             } 
  94.  
  95.             //修改 i=71的一项 
  96.             query = new BasicDBObject(); 
  97.             query.put("i", 71); 
  98.             BasicDBObject update = new BasicDBObject(); 
  99.             update.put("i", 710); 
  100.             DBObject dbobj = collection.findAndModify(query, update); 
  101.             System.out.println(dbobj); 
  102.              
  103.             //修改 i=72的一项 
  104.             query = new BasicDBObject(); 
  105.             query.put("i", 72); 
  106.             update = new BasicDBObject(); 
  107.             update.put("i", 720); 
  108.             WriteResult result = collection.update(query, update); 
  109.             System.out.println(result); 
  110.              
  111.             //删除i=61的项 
  112.             query = new BasicDBObject(); 
  113.             query.put("i", 61); 
  114.             collection.findAndRemove(query); 
  115.             //删除i=62的项 
  116.             BasicDBObject remove = new BasicDBObject(); 
  117.             remove.put("i", 62); 
  118.             collection.remove(remove); 
  119.              
  120.             //创建索引 1为升序、-1为降序 
  121.             collection.createIndex(new BasicDBObject("i", 1));  // create index on "i", ascending 
  122.  
  123.             //获取索引列表 
  124.             List<DBObject> list = collection.getIndexInfo(); 
  125.             for (DBObject o : list) { 
  126.                 System.out.println(o); 
  127.             } 
  128.  
  129.             //获取数据库列表 
  130.             for (String s : m.getDatabaseNames()) { 
  131.                 System.out.println(s); 
  132.             } 
  133.             //获取集合列表 
  134.             Set<String> colls = db.getCollectionNames(); 
  135.             for (String s : colls) { 
  136.                 System.out.println(s); 
  137.             } 
  138.  
  139.             //删除数据库 
  140.             //m.dropDatabase("my_new_db"); 
  141.              
  142.  
  143.         } catch (UnknownHostException e) { 
  144.             // TODO Auto-generated catch block 
  145.             e.printStackTrace(); 
  146.         } 
  147.     } 
posted @ 2014-07-15 09:14  xiangfeng2318  阅读(137)  评论(0编辑  收藏  举报