MongoDB实现增删查方法
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实现增删查方法的更多相关文章
- MongoDB的增删查改基本操作
MongoDB的增删查改基本操作 先决条件建库.建集合.建文档 连接mongo,如果连接不上什么连接拒绝,输入mongod命令,启动服务后 输入mongo show dbs 显示当前的所有的数据库 一 ...
- MongoDB入门学习(三):MongoDB的增删查改
对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改. 由于M ...
- mongodb+nodejs 增删查的demo
1.启动数据库 启动完成后显示 端口号是27017 2.创建数据库 创建一个名为mydb的数据库 3.先查询一下当然的用户,再新增一个 4.创建数据表,查询所有的表 db.createCollec ...
- 8天学通MongoDB——第二天 细说增删查改
原文地址:http://www.cnblogs.com/huangxincheng/archive/2012/02/19/2357846.html 看过上一篇,相信大家都会知道如何开启mongodb了 ...
- MongoDB数据库进阶 --- 增删查改...
注意: monogdb数据在使用之后必须及时 mongodb.close()否则后台崩溃. 在之前的文章中,我已经介绍了什么事MongoDB以及怎么在windows下安装MongoDB等等基本知识. ...
- SSH框架的多表查询和增删查改 (方法一)上
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==> http://www.cnblogs.com/zhu520/p/7772823.html 因为最近在做Android 练习的 ...
- SSH框架的多表查询和增删查改 (方法一)中
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html 这边文章是接的刚刚前一遍的基础上敲的 ...
- SSH框架的多表查询(方法二)增删查改
必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html 一:在前一个方法(http://www.cnblogs.com/zhu520/p ...
- MongoDB数据库(二):增删查改
MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...
随机推荐
- 关于 if __name__ == '__main__':的使用
当是用 if __name__ == '__main__': 时,下面调用函数只是在当前脚本调试, 而我们需要在别处导入这个脚本中的类或者函数时,这个if __name__ == '__main__ ...
- java:LeakFilling (SQL,JDBC)
1.JDBC中的sql里面不能加 :号,否则报错 2.Oracle数据必须提交后才可以使用JDBC进行操作,否则没有结果 3. JDBC插入序列: 首先在sequences建一个序列 insert i ...
- github局部不同图片合并插件
用于解决游戏开发时,一套图里有局部地区图片不同其他地方相同,导致资源重复过大的问题 地址:https://github.com/Elringus/SpriteDicing
- vue ----》实现打印功能
1.安装打印相关依赖 cnpm install vue-print-nb --save 2.安装后,在main.js文件中引入 import Print from 'vue-print-nb' Vue ...
- Linux下面MariaDB 管理命令基础使用
MariaDB 是 MySQL 的一个分,由于某些原因,使之取代了Mysql成为了 RHEL/CentOS 7 的默认数据库.针对数据库的操作我们经常做的操作就是增删查改,接下来就介绍下 MariaD ...
- mysql中char,varchar与text类型的区别
关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... text ...
- JackRabbit的来源
题记 写这系列有点老调重弹的味道,比如ahuaxuan已经在他的博客里对于JackRabbit 1.0做了很详细的阐述.之所以再写,是因为JCR推出了JCR 2.0,个人觉得有必要将一些新的特性再罗列 ...
- java 集合 队列(Queue)
特点:特殊线性表,先进先出(FIFO first-in-first-out) 方法: java5中 添加java.util.Queue接口,java.util.Collection 的扩展类 add( ...
- laravel修改用户模块的密码验证
做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users 之外的用户表进行认证,那么就需要多做一点工作来完成这个功能. 现在假设我们只需要修改登录用户的表,表名和表结构 ...
- GitHub编辑README.md
一.标题 等级表示法(六级): #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 一级标题/大标题(文本下面加上等于号): 大标题 === 二级标题 ...