认识Mongodb及其Java的连接

什么是Mongodb?

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

优点

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

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

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

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

缺点

1.MongoDB 不支持事务

2. 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();

查看库,查看集合

//查看链接的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);
}

添加数据

//插入一条数据

//存入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);

删除数据

//删除一条数据

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没有匹配数组中的规定值。
//修改多条数据
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();

查询数据

//全查
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();

模糊查询

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();

分页查询

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();

排序查询

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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值