当前位置: 移动技术网 > IT编程>数据库>MongoDB > mongodb driver使用代码详解

mongodb driver使用代码详解

2020年05月12日  | 移动技术网IT编程  | 我要评论

杨昕一,绑匪给家属打电话,上海楼市最新动态

mongodb 是一个基于分布式文件存储的数据库。由 c++ 语言编写。旨在为 web 应用提供可扩展的高性能数据存储解决方案。

mongodb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

0 前言

全是干货的技术殿堂

文章收录在我的 github 仓库,欢迎star/fork:

java-interview-tutorial

https://github.com/wasabi1234/java-interview-tutorial

mongodb-driver是mongo官方推出的java连接mongodb的驱动包,相当于jdbc驱动。我们现在来使用mongodb-driver完成对mongodb的操作。

1 环境准备

创建工程,并添加以下依赖:

<dependency> 
 <groupid>org.mongodb</groupid> 
 <artifactid>mongodb-driver</artifactid> 
 <version>3.10.1</version> 
</dependency>

2 使用mongodb-driver

2.1 查询所有

@test 
public void test1() { 
 //创建连接 
 mongoclient client = new mongoclient("192.168.200.128");
 //打开数据库 
 mongodatabase commentdb = client.getdatabase("commentdb"); 
 //获取集合 
 mongocollection<document> comment = commentdb.getcollection("comment"); 
 //查询 
 finditerable<document> documents = comment.find(); 
 //查询记录获取文档集合 
 for (document document : documents) { 
 system.out.println("_id:" + document.get("_id")); 
 system.out.println("内容:" + document.get("content")); 
 system.out.println("用户id:" + document.get("userid")); 
 system.out.println("点赞数:" + document.get("thumbup")); }
 //关闭连接 
 client.close(); 
 }
 } 

2.2 根据_id查询

每次使用都要用到mongocollection,进行抽取:

private mongoclient client; 
private mongocollection<document> comment; 
@before 
public void init() { 
 //创建连接 
 client = new mongoclient("192.168.200.128"); 
 //打开数据库 
 mongodatabase commentdb = client.getdatabase("commentdb"); 
 //获取集合 
 comment = commentdb.getcollection("comment"); 
}
@after 
public void after() { 
 client.close(); 
}
@test public void test2() { 
 //查询 
 finditerable<document> documents = comment.find(new basicdbobject("_id", "1")); 
 //查询记录获取文档集合 
 for (document document : documents) { 
 system.out.println("_id:" + document.get("_id")); 
 system.out.println("内容:" + document.get("content")); 
 system.out.println("用户id:" + document.get("userid")); 
 system.out.println("点赞数:" + document.get("thumbup")); 
 } 
}

2.3 新增

@test public void test3() { 
 map<string, object> map = new hashmap(); 
 map.put("_id", "6"); 
 map.put("content", "很棒!"); 
 map.put("userid", "9999"); 
 map.put("thumbup", 123); 
 document document = new document(map); 
 comment.insertone(document); 
}

2.4 修改

@test public void test4() { 
 //修改的条件 
 bson filter = new basicdbobject("_id", "6"); 
 //修改的数据 
 bson update = new basicdbobject("$set", new document("userid", "8888"));
 comment.updateone(filter, update); 
}

2.5 删除

@test public void test5() { 
 //删除的条件 
 bson filter = new basicdbobject("_id", "6"); 
 comment.deleteone(filter); 
}

mongodb优势与劣势

优势:

1、在适量级的内存的mongodb的性能是非常迅速的,它将热数据存储在物理内存中,使得热数据的读写变得十分快。
2、mongodb的高可用和集群架构拥有十分高的扩展性。
3、在副本集中,当主库遇到问题,无法继续提供服务的时候,副本集将选举一个新的主库继续提供服务。
4、mongodb的bson和json格式的数据十分适合文档格式的存储与查询。

劣势:

1、 不支持事务操作。mongodb本身没有自带事务机制,若需要在mongodb中实现事务机制,需通过一个额外的表,从逻辑上自行实现事务。
2、 应用经验少,由于nosql兴起时间短,应用经验相比关系型数据库较少。
3、mongodb占用空间过大。

总结

到此这篇关于mongodb driver使用代码详解的文章就介绍到这了,更多相关mongodb driver使用 内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网