认识Mongodb及其Java的连接

什么是Mongodb?

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

优点

1.MongoDB的提供了一个面向文档存储,操作起来比较简单和容易。

2.如果负载的增加,它可以分布在计算机网络中的其他节点上这就是所谓的分片。

  1. MongoDB支持各种编程语言:RUBY,PYTHON,JAVA,C++, PHP,C#等多种语言。

4.你可以通过本地或者网络创建数据镜像,这使得MongoDB有更强的扩展性。

缺点

1.MongoDB 不支持事务

  1. MongoDB 不能进行多表联查

为什么学习MongoDB

MongoDB解决Mysql 的"三高"问题:

1.对数据库高并发写入需求

2.对海量数据高效率存储访问需求

3.对数据库高扩展和高可用的需求

MongoDB 实际应用:

1.社交场景,比如朋友圈,附近的人的地点的存储

2.游戏场景,比如用户当前装备,得分等

3.物流场景,比如快递的位置,状态,途径

4.视频场景,比如直播中的点赞数和互动留言等

接下来让我们去了解一下使用Java操作MongoDB

Java链接MongoDB

导入MongoDB驱动包

获取链接对象

MongoClient mongoclient = new Mongoclient("localhost", 27017);

关闭链接

mongoclient.close();

查看库,查看集合

java 复制代码
//查看链接的MongoDB中的所有的库

MongoIterable<String>dbslist = mongoclient.listDatabaseNames();

for(string db :dbslist){
System.out.println(db);
}

//使用库查看库中的集合
MongoDatabase bbsDB = mongoclient.getDatabase("bbs");

MongoIterable<String>collist =bbsDB.listCollectionNames();

for(string s:collist){
System.out.println(s);
}

添加数据

java 复制代码
//插入一条数据

//存入MongoDB的数据
Comment com=new comment();
com.setcontent("专家说空腹不宜吃早餐");
com.setPublishtime(new Date());
// 将数据转换为json格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String json=gson.toJson(com);
// 获取集合对象
MongoCollection<Document>commCol =bbsDB.getcollection("comment");
//添加一条数据--将json格式转换为decument对象
commCol.insertOne(Document.parse(json));

//插入多条数据

//存入MongoDB的数据
List<Document>dlist =new ArrayList<Document>();
//需要的数据
for(int i=0;i<5;i++){
    Comment com=new Comment();
    com.setId(Integer.tostring(i+1));
    com.setcontent("专家说空腹不宜吃早餐");
    com.setPublishtime(new Date());
    // 将数据转换为ison格式
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    String json =gson.toJson(com);
    dlist.add(Document.parse(json);
}
//获取集合对象
Mongocollection<Document>commCol = bbsDB.getcollection("comment");
// 添加多条数据
commCol.insertMany(dlist);

删除数据

java 复制代码
//删除一条数据

MongoCollection<Document>commCol = bbsDB.getCollection("comment");
Comment com =new Comment();
com.setId("1");
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
Bson bson =Document.parse(gson.to]son(com));
DeleteResult deleteOne =commCol.delete0ne(bson);
if(delete0ne.getDeletedcount()>0){
    System.out.println("删除成功");
}else{
    System.out.println("删除失败");
}

// 删除多条数据
MongoCollection<Document>commCol = bbsDB.getCollection("comment");
Comment com =new omment();
com.setcontent("专家说空腹不宜吃早餐");
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
Bson bson =Document.parse(gson.to]son(com));
DeleteResult deleteMany=commCol.deleteMany(bson);
if(deleteMany.getDeletedcount()>0){
    System.out.println("删除成功");
}else {
    System.out.println("删除失败");
}

修改数据

|---------|----------------|
| eq | 匹配等于指定值的值。 |
| gt | 匹配大于指定值的值。 |
| gte | 匹配大于或等于指定值的值。 |
| lt | 匹配小于规定值的值。 |
| lte | 匹配是小于或等于规定值的值。 |
| ne | 匹配不等于指定值的所有值。 |
| in | 匹配任何在数组中指定的值。 |
| nin | 没有匹配数组中的规定值。 |

java 复制代码
//修改多条数据
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
// 修改数据
MongoCollection<Document>commcol =bbsDB.getcollection("comment");
// 条件
Bson b1 = Filters.eq("content","专家说空腹不宜吃早餐");
System.out.println(b1);
//修改的内容
Comment comup =new Comment();
comup.setLikenum(1);
Bson b2 = new Document("$inc",Document.parse(gson.to]son(comup)));
System.out.println(b2);
// 修改多条数据
UpdateResult updateMany=commCol.updateMany(b1,b2);
System.out.println(updateMany);
if(updateMany.getModifiedcount()>0){
    System.out.println("修改成功");
}else {
    System.out.println("修改失败");
}
Hmongoclient.close();

查询数据

java 复制代码
//全查
Mongocollection<Document> commCol = bbsDB.getcollection("comment");
FindIterable<Document> bbses = commcol.find();
MongoCursor<Document> iterator = bbses.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next());
}
mongoclient.close();

/带多个条件查询
Mongocollection<Document> commCol = bbsDB.getCollection("comment");
Bson b1 = Filters.and(
        Filters.eg("content""专家说空腹不宜吃早餐"),
        Filters.gte("likenum",7)
    );
FindIterable<Document>bbses = commcol.find(b1);
MongoCursor<Document>iterator=bbses.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next());
}
mongoclient.close();

模糊查询

java 复制代码
MongoCollection<Document>commCol = bbsDB.getcollection("comment");
//使用正则表达式进行模糊查找
Bson b1 = Filters.regex("content","不能");
FindIterable<Document>bbses = commcol.find(b1);
MongoCursor<Document>iterator=bbses.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next());
}
mongoclient.close();

分页查询

java 复制代码
Mongocollection<Document>>commCol = bbsDB.getcollection("comment");
// 分页查询
FindIterable<Document>bbses = commcol.find().skip(2).limit(3);
Mongocursor<Document>iterator=bbses.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next());
}
mongoclient.close();

排序查询

java 复制代码
Mongocollection<Document>commCol = bbsDB.getcollection("comment");
// 排序1 表示升序 -1 表示降序
Bson bl = new Document("id",-1);
FindIterable<Document>bbses = commcol.find().sort(b1);
MongoCursor<Document>iterator = bbses.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next());
}
mongoclient.close();
相关推荐
小吴编程之路19 小时前
MySQL 索引核心特性深度解析:从底层原理到实操应用
数据库·mysql
~莫子20 小时前
MySQL集群技术
数据库·mysql
凤山老林20 小时前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
就不掉头发20 小时前
Linux与数据库进阶
数据库
与衫20 小时前
Gudu SQL Omni 技术深度解析
数据库·sql
咖啡の猫20 小时前
Redis桌面客户端
数据库·redis·缓存
oradh21 小时前
Oracle 11g数据库软件和数据库静默安装
数据库·oracle
what丶k21 小时前
如何保证 Redis 与 MySQL 数据一致性?后端必备实践指南
数据库·redis·mysql
_半夏曲21 小时前
PostgreSQL 13、14、15 区别
数据库·postgresql
把你毕设抢过来21 小时前
基于Spring Boot的社区智慧养老监护管理平台(源码+文档)
数据库·spring boot·后端