1.添加信息

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

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() );
} }

MongoDB实现增删查方法的更多相关文章

  1. MongoDB的增删查改基本操作

    MongoDB的增删查改基本操作 先决条件建库.建集合.建文档 连接mongo,如果连接不上什么连接拒绝,输入mongod命令,启动服务后 输入mongo show dbs 显示当前的所有的数据库 一 ...

  2. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  3. mongodb+nodejs 增删查的demo

    1.启动数据库 启动完成后显示 端口号是27017 2.创建数据库 创建一个名为mydb的数据库   3.先查询一下当然的用户,再新增一个 4.创建数据表,查询所有的表 db.createCollec ...

  4. 8天学通MongoDB——第二天 细说增删查改

    原文地址:http://www.cnblogs.com/huangxincheng/archive/2012/02/19/2357846.html 看过上一篇,相信大家都会知道如何开启mongodb了 ...

  5. MongoDB数据库进阶 --- 增删查改...

    注意: monogdb数据在使用之后必须及时 mongodb.close()否则后台崩溃. 在之前的文章中,我已经介绍了什么事MongoDB以及怎么在windows下安装MongoDB等等基本知识. ...

  6. SSH框架的多表查询和增删查改 (方法一)上

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的 ...

  7. SSH框架的多表查询和增删查改 (方法一)中

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html   这边文章是接的刚刚前一遍的基础上敲的  ...

  8. SSH框架的多表查询(方法二)增删查改

     必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html  一:在前一个方法(http://www.cnblogs.com/zhu520/p ...

  9. MongoDB数据库(二):增删查改

    MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...

随机推荐

  1. self-training and co-training

    半指导学习(Semi-supervised Learning)的概念说起来一点儿也不复杂,即从同时含有标注数据和未标注数据的训练集中学习模型.半指导学习是介于有指导学习与无指导学习之间的一种机器学习方 ...

  2. java中String中的endsWith()方法

    解释:endsWith() ——此方法测试字符串是否以指定的后缀 suffix 结束. 此方法的定义:public boolean endsWith(String suffix) 我这里判断的是路径是 ...

  3. USACO4.3 Street Race【分析】

    这道题,感觉不是很难,分析清楚之后非常简单.(标签都不知道怎么加) 读完题首先想到了分割点一定是必经点的一种特殊情况,如果分割点不是必经点的话,那么它就不能把这个图分成两半(存在不经过它的边沟通两半) ...

  4. 【VS开发】【智能语音处理】特定人语音识别算法—DTW算法

    DTW(动态时间弯折)算法原理:基于动态规划(DP)的思想,解决发音长短不一的模板匹配问题.相比HMM模型算法,DTW算法的训练几乎不需要额外的计算.所以在孤立词语音识别中,DTW算法仍得到广泛的应用 ...

  5. Android引用多媒体

    res目录下,创建raw目录(Android会自动识别这个目录),如果自己创建的目录,可能无效底下的mp3格式,mp4格式的文件名必须小写. 引用方式: mediaPlayer = MediaPlay ...

  6. 内核中likely和unlikely宏定义

    在内核代码中经常会看到unlikely和likely的踪影.他们实际上是定义在 linux/compiler.h 中的两个宏. #define likely(x)    __builtin_expec ...

  7. JAVA重写不需要@override

    一,如下代码, package com.boot.enable.bootenable; import org.springframework.scheduling.annotation.Async; ...

  8. 8.jQuery之上下滑动效果

    上下滑动:slideDown   slideUp  slideToggle <style> div { width: 150px; height: 300px; background-co ...

  9. Vue中的组件直接的通信是如何实现的

    组件关系可分为父子组件通信.兄弟组件通信 1.父组件传子组件 通过props属性来实现 2.子组件传父组件 子组件用$emit()来触发事件,父组件用$on()来监听子组件的事件 3.兄弟之间的通信 ...

  10. JS 的 Document对象

    Document 对象是是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. 当浏览器载入 HTML 文档, 它就会成为 Document 对象. Document对象 ...