今天敲代码了吗?   

MongoDB实现增删查方法

1.添加信息

 1 public void addInfo(Infomation infomation) {
 2         try{
 3         // TODO Auto-generated method stub
 4          //连接MongoDB,指定连接数据库名,指定连接表名。
 5         MongoCollection<Document> collection= getCollection("importent","infomation");    //数据库名:School 集合名:student
 6         //实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加append
 7         Document doc1=new Document("username",infomation.getUsername())
 8                 .append("ptnum", infomation.getPtnum())
 9                 .append("date", infomation.getDate())
10                 .append("wh", infomation.getWh())
11                 .append("toc", infomation.getToc())
12                 .append("lelev", infomation.getLelev()).append("dqu", 
13                 infomation.getDqu()).append("pttype", infomation.getPttype()).append("jingjgj", infomation.getJingjgj())
14                 .append("imxueke", infomation.getImxueke()).append("dwname", infomation.getDwname())
15                 .append("jgdaima", infomation.getJgdaima())
16                 .append("fname", infomation.getFname())
17                 .append("phone", infomation.getPhone())
18                 .append("type", infomation.getType())
19                 .append("dw", infomation.getDw()).append("zname", infomation.getZname()).append("zsex", infomation.getZsex())
20                 .append("zbir", infomation.getZbir()).append("zzc", infomation.getZzc()).append("zzy", infomation.getZzy())
21                 .append("zxl", infomation.getZxl())
22                 .append("zxw", infomation.getZxw())
23                 .append("zphone", infomation.getZphone())
24                 .append("zemail", infomation.getZemail())
25                 .append("wzname", infomation.getWzname())
26                 .append("wz", infomation.getWz())
27                 .append("txaddress", infomation.getTxaddress())
28                 .append("mail", infomation.getMail())
29                 .append("ptt", infomation.getPtt())
30                 .append("yphone", infomation.getYphone());
31         //实例化一个文档,文档内容为{sname:'Bob',sage:20}
32         //Document doc2=new Document("_id","2").append("sage", 20).append("sname", "bbb");
33         List<Document> documents = new ArrayList<Document>(); 
34         //将doc1、doc2加入到documents列表中
35         documents.add(doc1); 
36         //documents.add(doc2); 
37         //将documents插入集合
38         collection.insertMany(documents);  
39         System.out.println("插入成功"); 
40     }catch(Exception e){
41         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
42     }
43         
44 }

2.查询信息

public List<Infomation> loadInfo() {
        List<Infomation> infomations =new ArrayList<Infomation>();
        Infomation infomation =null;
        try{
           MongoCollection<Document> collection = getCollection("importent","infomation");  //数据库名:School 集合名:student
           //通过游标遍历检索出的文档集合 
         //MongoCursor<Document>  cursor= collection.find(new Document("sname","1")). projection(new Document("sname",1).append("sage",1).append("_id", 0)).iterator();   //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)
           //查询所有数据
           MongoCursor<Document>  cursor= collection.find().iterator();
//           while(cursor.hasNext()){
//              // System.out.println(cursor.next().toJson());
//               System.out.println(((Document) cursor).get("username"));
//           }
         for(Document cur :collection.find()) {
             infomation=new Infomation();
            infomation.setUsername(cur.get("username").toString());
            infomation.setPtnum(cur.get("ptnum").toString());
           infomation.setDate(cur.get("date").toString());
           infomation.setWh(cur.get("wh").toString());
           infomation.setToc(cur.get("toc").toString());
           infomation.setLelev(cur.get("lelev").toString());
           infomation.setDqu(cur.get("dqu").toString());
           infomation.setPttype(cur.get("pttype").toString());
           infomation.setJingjgj(cur.get("jingjgj").toString());
           infomation.setImxueke(cur.get("imxueke").toString());
           infomation.setDwname(cur.get("dwname").toString());
           infomation.setFname(cur.get("fname").toString());
           infomation.setPhone(cur.get("phone").toString());
           infomation.setType(cur.get("type").toString());
           infomation.setDw(cur.get("dw").toString());
           infomation.setZname(cur.get("zname").toString());
           infomation.setZsex(cur.get("zsex").toString());
           infomation.setZbir(cur.get("zbir").toString());
           infomation.setZzc(cur.get("zzc").toString());
           infomation.setZzy(cur.get("zzy").toString());
           infomation.setZxl(cur.get("zxl").toString());
           infomation.setZxw(cur.get("zxw").toString());
           infomation.setZphone(cur.get("zphone").toString());
           infomation.setZemail(cur.get("zemail").toString());
           infomation.setWzname(cur.get("wzname").toString());
           infomation.setWz(cur.get("wz").toString());
           infomation.setTxaddress(cur.get("txaddress").toString());
           infomation.setMail(cur.get("mail").toString());
           infomation.setPtt(cur.get("ptt").toString());
           infomation.setYphone(cur.get("yphone").toString());
              System.out.println(cur.get("username").toString());
              System.out.println(infomation.getUsername());
              infomations.add(infomation);
              System.out.println(infomation.getUsername());
              }
       }catch(Exception e){
           System.err.println( e.getClass().getName() + ": " + e.getMessage() );
       }
        return infomations;
    }

3.删除

    public void delete(String username ) {
         try{
                MongoCollection<Document> collection = getCollection("importent","infomation");  //数据库名:School 集合名:student
                    //删除符合条件的第一个文档  
                collection.deleteOne(Filters.eq("type", username));  
                //删除所有符合条件的文档  
                //collection.deleteMany (Filters.eq("sname", "Bob"));
                System.out.println("删除成功!");
            }catch(Exception e){
                System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            }
        
    }

 

posted on 2018-11-06 19:33  今天学算法了吗?  阅读(167)  评论(0编辑  收藏  举报

导航