认识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();
相关推荐
头顶秃成一缕光3 分钟前
Redis的主从模式和哨兵模式
数据库·redis·缓存
AIGC大时代5 分钟前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
博睿谷IT99_6 分钟前
数据库证书可以选OCP认证吗?
数据库·oracle·开闭原则·ocp认证
乐维_lwops18 分钟前
数据库监控 | MongoDB监控全解析
数据库·mongodb·数据库监控
观无18 分钟前
Redis安装及入门应用
数据库·redis·缓存
柏油1 小时前
MySql InnoDB 事务实现之 undo log 日志
数据库·后端·mysql
DolphinScheduler社区2 小时前
白鲸开源WhaleStudio与崖山数据库管理系统YashanDB完成产品兼容互认证
数据库·开源·认证·崖山数据库·白鲸开源
阑梦清川2 小时前
AI超级智能体项目教程(二)---后端项目初始化(设计knif4j接口文档的使用)
java·前端·数据库
hotlinhao2 小时前
ThinkPHP6模型中多组条件逻辑或Or查询的使用
linux·服务器·数据库
jack xu12 小时前
高频面试题:如何保证数据库和es数据一致性
java·大数据·数据库·mysql·elasticsearch