• 6. MongoDB——Java操作(增删改查)


    转自:https://blog.csdn.net/kai402458953/article/details/79626148

      1 import java.net.UnknownHostException;  
      2     import java.util.Iterator;  
      3     import java.util.Set;  
      4       
      5     import com.mongodb.BasicDBObject;  
      6     import com.mongodb.DB;  
      7     import com.mongodb.DBCollection;  
      8     import com.mongodb.DBCursor;  
      9     import com.mongodb.DBObject;  
     10     import com.mongodb.Mongo;  
     11     import com.mongodb.MongoException;  
     12       
     13     public class MongoMain {  
     14           
     15         static DB db = null;  
     16         static DBCollection coll = null;  
     17           
     18         static {  
     19              Mongo m = null;  
     20             try {  
     21                 //m = new Mongo();//默认本地  
     22                 //m = new Mongo("127.0.0.1");//默认端口  
     23                 m = new Mongo("127.0.0.1",27017);  
     24             } catch (UnknownHostException e) {  
     25                 e.printStackTrace();  
     26             } catch (MongoException e) {  
     27                 e.printStackTrace();  
     28             }  
     29               
     30             //获取名为 test 的数据库,不存在的情况下创建  
     31             db = m.getDB("test");  
     32               
     33             //登录数据库(用户名:test,密码:test)  
     34             boolean auth = db.authenticate("test", "test".toCharArray());  
     35             if(auth){  
     36                 //只有admin中的用户才有此权限(show dbs)  
     37                 //获取所有数据库,不显示无collection的db  
     38                 //System.out.println("所有数据库名:"+m.getDatabaseNames());  
     39                   
     40                 //获取名为 user 的collection(相当于表),不存在的情况下创建  
     41                 coll = db.getCollection("user");  
     42             }else{  
     43                 System.out.println("登录失败!");  
     44             }  
     45         }  
     46           
     47         public static void main(String[] args) {  
     48             MongoMain test = new MongoMain();  
     49             if(coll!=null){  
     50                 test.saveData();  
     51                 test.searchData();  
     52                 test.updateData();  
     53                 test.deleteData();  
     54             }  
     55         }  
     56       
     57         /** 
     58          * 保存数据 
     59          */  
     60         public void saveData(){  
     61             //录入学生1的信息  
     62             BasicDBObject stu1 = new BasicDBObject();  
     63             stu1.put("name", "jack");  
     64             stu1.put("age", "25");  
     65             BasicDBObject sight1 = new BasicDBObject();  
     66             sight1.put("left", 1.5);  
     67             sight1.put("right", 1.2);  
     68             stu1.put("sight", sight1);  
     69               
     70             //录入学生2的信息  
     71             BasicDBObject stu2 = new BasicDBObject();  
     72             stu2.put("name", "lucy");  
     73             stu2.put("age", "22");  
     74             BasicDBObject sight2 = new BasicDBObject();  
     75             sight2.put("left", 1.0);  
     76             sight2.put("right", 1.3);  
     77             stu2.put("sight", sight2);  
     78               
     79             //注意:不能直接对sight赋值{left:1.0,right:1.3}  
     80               
     81             coll.insert(stu1);  
     82             coll.insert(stu2);  
     83         }  
     84           
     85         /** 
     86          * 查询数据 
     87          */  
     88         public void searchData(){  
     89             System.out.println("=======================");  
     90             //show collections  
     91             //获取数据库下所有的collection,不显示无数据的collection  
     92             Set<String> colls = db.getCollectionNames();  
     93             showData(colls);  
     94               
     95             System.out.println("=======================");  
     96               
     97             //查询coll中全部记录   
     98             DBCursor ite = coll.find();    
     99             showData(ite);  
    100               
    101             System.out.println("=======================");  
    102               
    103             //获取第一条记录  
    104             DBObject o = coll.findOne();  
    105             System.out.println(o);  
    106               
    107             System.out.println("=======================");  
    108               
    109             //统计colletion的数据条数  
    110             System.out.println(coll.getCount());  
    111               
    112             System.out.println("=======================");  
    113               
    114             //查询 name为jack的对象   
    115             BasicDBObject query = new BasicDBObject();  
    116             query.put("name", "jack");  
    117             DBCursor it = coll.find(query);    
    118             showData(it);  
    119               
    120             System.out.println("=======================");  
    121               
    122             //查询age小于30,age不等于20的对象   
    123             BasicDBObject query2 = new BasicDBObject();  
    124             query2.put("age", new BasicDBObject("$lt", 30));  
    125             query2.put("age", new BasicDBObject("$ne", 20));  
    126             DBCursor it2 = coll.find(query2);    
    127             showData(it2);  
    128         }  
    129           
    130         /** 
    131          * 修改数据 
    132          */  
    133         public void updateData(){  
    134             BasicDBObject query = new BasicDBObject();  
    135             query.put("name", "lucy");  
    136             //这里的new_info对象一定要是find出的而不是new的,否则多字段的情况下就会丢失其它字段信息  
    137             DBObject new_info = coll.findOne(query);  
    138               
    139             //方法一(缺点,必须把2个值都put进去)  
    140             BasicDBObject sight = new BasicDBObject();  
    141             sight.put("left", 1.3);  
    142             sight.put("right", 1.3);  
    143             new_info.put("sight", sight);  
    144               
    145             //方法二(优点,只需设置要修改的字段的值)  
    146             DBObject obj = (DBObject)new_info.get("sight");  
    147             obj.put("right", 1.5);  
    148               
    149             coll.update(query, new_info);  
    150         }  
    151           
    152         /** 
    153          * 删除数据 
    154          */  
    155         public void deleteData(){  
    156             BasicDBObject data = new BasicDBObject();  
    157             //删除名称为lucy的记录  
    158             data.put("name", "lucy");  
    159             //传入[空实例]删除所有  
    160             coll.remove(data);  
    161         }  
    162           
    163         /** 
    164          * 遍历显示结果 
    165          * @param result 
    166          */  
    167         @SuppressWarnings("rawtypes")   
    168         public void showData(Iterable result){  
    169             Iterator it = result.iterator();   
    170             while(it.hasNext())    
    171             {    
    172                 System.out.println(it.next());    
    173             }    
    174         }    
    175     }
  • 相关阅读:
    mysql 查看存储过程 并导出
    mysql 添加记录或者删除记录
    mysql 修改表的字段
    搭建docker私有仓库
    安装gitlab并配置邮箱
    Mac 安装MySQL-python
    android studio 调试安装
    给定日期求星期几
    数字三角形
    程序设计实训-课程表管理系统项目中遇到的问题
  • 原文地址:https://www.cnblogs.com/sharpest/p/7831020.html
Copyright © 2020-2023  润新知