mongodb driver使用代码详解
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使用 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用
- CListCtrl用法 CListCtrl使用详解 (代码)
- Spring整合JUnit框架进行单元测试代码使用详解
- SQLMETAL使用LINQ自动代码生成工具命令残参数详解
- extern C的作用详解(通过它可以在c++中使用c代码)
- java正则表达式语法详解及其使用代码实例
- Yii2中事务的使用实例代码详解
- php中使用parse_url()对网址进行解析的实现代码(parse_url详解)
- 360通用php防护代码(使用操作详解)
- Spring整合JUnit框架进行单元测试代码使用详解
- 提高Java代码质量的Eclipse插件之Checkstyle的使用详解
- WordPress中的shortcode短代码功能使用详解
- [置顶] Android java程序员必备技能,集合与数组中遍历元素,增强for循环的使用详解及代码
- Fresceo使用详解--用Java代码实现xml配置的效果
- mybatis-generator自动生成代码插件使用详解
- php中使用parse_url()对网址进行解析的实现代码(parse_url详解)
- WordPress代码高亮插件SyntaxHighlighter终极使用详解
- 提高Java代码质量的Eclipse插件之Checkstyle的使用详解
- php中使用parse_url()对网址进行解析的实现代码(parse_url详解)