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 ...
随机推荐
- 十八:jinja2之include标签
用于将页面的某一块地方抽取出来,要嵌入内容的时候使用,继承的概念 把具体内容分别放到其他地方同一管理,要用的时候使用include继承 使用include的时候可以直接使用接收的数据
- 报错:Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET
问题:Appium的android真机启动手机时,会遇到以下问题: An unknown server-side error occurred while processing the command ...
- c++ 运算符 循环
运算符 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 杂项运算符 一.算术运算符 二.关系运算符 三.逻辑运算符 四.位运算符 位运算符作用于位,并逐位执行操作 假设如果 A = 60,且 ...
- 服务器上安装并使用tensorboard
需求: 在ubunu16.0的服务器上使用Pytorch内嵌的tensorboard 安装 pip install tensorflow pip install tensorboardX 如果嫌慢可以 ...
- 论文翻译:HetConv-Heterogeneous Kernel-Based Convolutions for Deep CNNs
Abstract 我们提出了一种新颖的深度学习架构,其中卷积操作利用了异构内核.与标准卷积运算相比,所提出的HetConv(基于异构内核的卷积)减少了计算(FLOPs)和参数的数量,同时仍保持表示效率 ...
- 【神经网络与深度学习】Leveldb的一些具体操作说明
本文转自 http://blog.csdn.net/poweruser5956/article/details/7727325 Leveldb概述 leveldb提供了持久的键值对的存储.key和va ...
- 深入理解java:4. 框架编程
了解 Servlet 和 Filter Servlet(即servlet-api.jar) 是 J2EE 最重要的一部分,有了 Servlet 你就是 J2EE 了,J2EE 的其他方面的内容择需采用 ...
- 理解ES6的模块导入与导出
export export后必须跟语句, 何为语句, 如声明, for, if 等都是语句, export 不能导出匿名函数, 也不能导出某个已经声明的变量, 如: export const bar ...
- IDEA下集成tomcat7插件将tomcat内嵌到web项目中
新建一个maven web项目 修改pom.xml文件 <build> <plugins> <!-- 配置Tomcat插件: 就是本地部署,将tomcat 内嵌到 web ...
- 弹出ifream
top.$.jBox("iframe:"+'${ctx}/synopsis/hmlwxSynopsis/addItem', {title: "添加作品",w ...