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. MJRefresh实现请求数据列表不满一页,或者请求数据为空时,不显示footer文字

    最近自己负责的项目测试要求: 列表的数据不满一页的话就自动隐藏下面的“上拉加载更多”或是“到底了,没有更多数据”. 百度了一下,看了一篇博客的介绍实现: http://blog.csdn.net/gx ...

  2. CTF—攻防练习之HTTP—SQl注入(get)

    攻击机:192.168.32.152 靶机 :192.168.32.157 扫描靶机扫端口: 开放了ssh和80看下ssh版本有没有漏洞,searchsplot下,没有发现 dirb扫描下目录,有个a ...

  3. python--006

    一.函数的作用域 1.作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 例一: name='alex' def foo(): name='lhf' def bar(): print(na ...

  4. C++随笔(0)——关于const

    最近发现自己对const这一块其实不甚熟悉,所以复习一下const的相关知识点. 基本用法 const int bufSize = 512; 上面这样就可以将bufSize定义为常量,编译的时候编译器 ...

  5. hadop-eclipse-plugin导入plugins后没有mapreduce视图

    这种现象一般由于安装在eclipse\plugins下的插件没有导入的问题. 解决方法:把 eclipse\configuration\org.eclipse.update 删除掉.出现这种情况的原因 ...

  6. 微信小程序 ----- this.getOpenerEventChannel is not a function

    小程序 添加新的功能, 页面跳转后,通过事件的发布订阅,实现 from => to 或者 to=> from 数据传递 1. 跳转到指定页面. 通过 wx.navigateTo() .请参 ...

  7. Java 浮点数的范围和精度

    本篇先介绍IEEE754标准中针对浮点数的规范,然后以问答形式补充有关浮点数的知识点. (一)IEEE754标准 IEEE 754 标准即IEEE浮点数算术标准,由美国电气电子工程师学会(IEEE)计 ...

  8. Maven从入门到精通(一)

    maven是一个项目管理工具,我的后续将会根据这个思维导图给大家由浅到深讲解Maven是如何管理我们的项目,以及我们如何更好的使用Maven maven在开发过程中功能主要有三个方面:   管理依赖 ...

  9. 常用DOS命令及编程软件fa

    1.常用的dos命令(应用) 在接触集成开发环境之前,我们需要使用命令行窗口对Java程序进行编译和运行,所以需要知道dos命令. 打开命令行窗口的方式:win + r打开运行窗口对java程序进行编 ...

  10. tp5框架用foreach循环时候报Indirect modification of overloaded element of think\paginator\driver\Bootst错误

    thinkphp5使用paginator分页查询数据后,需要foreach便利处理某一字段的数据,会出现类似题目的错误.主要是因为tp5使用分页类读取的数据不是纯数组的格式!所以在循环的时候需要用数据 ...