MongoDB详解(2)--java中的使用

MongoDB详解(2)

MongoDB的使用

mongo的连接和使用

连接mongo
java 复制代码
//连接mongo
MongoClient mc = new MongoClient("localhost",27017);

MongoIterable<String> ldb = mc.listDatabaseNames(); //获取mongo数据库列表
获取库
java 复制代码
//获取库中集合		
MongoDatabase db = mc.getDatabase("myschool"); //很重要常用

//获取库中的所有集合
MongoIterable<String> listIterable = db.listCollectionNames(); 
获取student集合
java 复制代码
//获取student集合
MongoCollection<Document> col = db.getCollection("student");
创建Doc对象,操作mongo
java 复制代码
//创建doc对象
Document doc = new Document();
doc.append("username", "jx");

//添加一条数据
col.insertOne(doc);
释放资源
java 复制代码
//释放资源
mc.close();

对mongo的操作

添加

1.添加一个doc

java 复制代码
//insert(一个document)
col.insertOne(doc);

2.添加多个

java 复制代码
//创建doc集合
List<Document> doclist = new ArrayList<Document>();
doclist.add(new Document("username", "jx好"));
doclist.add(new Document("username", "jx大"));
doclist.add(new Document("username", "jx坏"));
doclist.add(new Document("username", "jx怪"));
doclist.add(new Document("username", "jx乖"));
doclist.add(new Document("username", "jx怂"));
doclist.add(new Document("username", "jx强"));
doclist.add(new Document("username", "jx猛"));

//insert(一堆document)
col.insertMany(doclist);
删除
java 复制代码
//创建bson
//Bson bson = Filters.lt("age", 20);

//删除
Document bson = new Document();
bson.append("username", "jx");

//删除一条数据
DeleteResult r = col.deleteOne(bson);

//删除多条数据
DeleteResult r = col.deleteMany(bson);
修改
java 复制代码
//修改数据
Bson b1 = Filters.eq("name", "惠普小孩");
Document b2 = new Document("$set",new Document("age",18));


UpdateResult r = col.updateOne(
new Document("name","老张"), //条件
new Document("$set",new Document("age",18))); //修改的值

UpdateResult r = col.updateMany(b1, b2,new UpdateOptions().upsert(true));	
查询
java 复制代码
Gson gson = new Gson(); //将JSON转换为java中的实体类
ArrayList<Student> sList = new ArrayList<Student>();

// 全查
FindIterable<Document> find = col.find();

// 条件查询
FindIterable<Document> find = col.find(new Document("name","李四"));

// 多个条件
FindIterable<Document> find = col.find(Filters.and(Filters.gt("age", 10),Filters.eq("sex",true)));

// 模糊查询
FindIterable<Document> find = col.find(Filters.regex("name", "张"));

// 分页
FindIterable<Document> find = col.find().skip((1-1)*3).limit(3);

//排序
FindIterable<Document> find = col.find().sort(new Document("age",1));

for (Document doc : find) {
    String json = doc.toJson(); //获取的数据转换成json
    Student s = gson.fromJson(json, Student.class); //通过gson将JSON字符串转换为对象
    sList.add(s);
}

for (Student s : sList) {
    System.out.println(s);
}
相关推荐
程序员清风11 小时前
快手二面:乐观锁是怎么用它来处理多线程问题的?
java·后端·面试
一线大码11 小时前
SpringBoot 优雅实现接口的多实现类方式
java·spring boot·后端
花伤情犹在11 小时前
Java Stream 高级应用:优雅地扁平化(FlatMap)递归树形结构数据
java·stream·function·flatmap
yaoxin52112312 小时前
212. Java 函数式编程风格 - Java 编程风格转换:命令式 vs 函数式(以循环为例)
java·开发语言
摇滚侠12 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 属性优先级 行内写法 变量选择 笔记42
java·spring boot·笔记
滑水滑成滑头12 小时前
**发散创新:多智能体系统的探索与实践**随着人工智能技术的飞速发展,多智能体系统作为当今研究的热点领域,正受到越来越多关注
java·网络·人工智能·python
摇滚侠12 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 总结 热部署 常用配置 笔记44
java·spring boot·笔记
十年小站12 小时前
一、新建一个SpringBoot3项目
java·spring boot
2401_8414956412 小时前
【数据结构】最长的最短路径的求解
java·数据结构·c++·python·算法·最短路径·图搜索
麦麦鸡腿堡12 小时前
Java的代码块介绍与快速入门
java·开发语言