目录
什么是MongoDB
优缺点
应用场景
对数据库&集合(表)的基本操作
MongoDB的直接操作
#1.创建库
#2.查看所有的库
#3.查看当前库
#4.删除库(危险操作)
#5.创建集合
#6.查看所有集合
#7.删除集合(危险操作)
使用Java操作MongoDB
#1.导入MongoDB驱动jar包
#2.获取Mongo链接对象
#3.查看库所有库
#4.拿到库对象
#5.查看所有集合
#6.获取集合对象
操作集合中的数据
1)新增
MongoDB的直接操作
使用Java操作MongoDB
2)查询
MongoDB的直接操作
使用Java操作MongoDB
3)修改
MongoDB的直接操作
使用Java操作MongoDB
4)删除(危险操作)
MongoDB的直接操作
使用Java操作MongoDB
什么是MongoDB
是一种用C++编写,为Web应用提供可扩展的高性能数据存储的解决方案,介于关系型数据库与NOSQL之间,是NOSQL中功能最丰富,最像关系数据库的。
优缺点
优点 | 缺点 |
面向文档存储、操作方便 | 不支持事务 |
高扩展性、支持分布式 | |
支持各种编程语言 | 不能进行多表联查 |
拷贝、克隆数据容易 |
应用场景
社交场景 | 朋友圈、附近的人 |
游戏场景 | 用户当前装备、得分 |
物流场景 | 快递的位置、状态、途经 |
视频场景 | 视频的点赞数与评论留言 |
对数据库&集合(表)的基本操作
MongoDB的直接操作
#1.创建库
use 库名;
#2.查看所有的库
show dbs;
#3.查看当前库
db;
MongoDB中的db相当于java中的this
#4.删除库(危险操作)
db.dropDatabase();
MongoDB的语法通过 " . " 调用方法
#5.创建集合
db.createCollection("集合名");
#6.查看所有集合
//方式一
show collections;
//方式二
show tables;
#7.删除集合(危险操作)
db.集合名.drop();
使用Java操作MongoDB
#1.导入MongoDB驱动jar包
#2.获取Mongo链接对象
//MongoClient mc = new MongoClient("IP地址",端口号);
MongoClient mc = new MongoClient("localhost",27017);
MongoDB的默认端口号为27017
注意:Mongo链接对象也是一种资源,需要在业务最后进行资源释放
mc.close()
#3.查看库所有库
//使用mongo类型的迭代器
MongoIterable<String> listDatabaseNames = mc.listDatabaseNames();
//获取迭代器对象
MongoCursor<String> iterator = listDatabaseNames.iterator();
//使用while循环遍历(也可使用增强for循环)
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
查看库的目的是查看库名,为后续拿库对象做基础
#4.拿到库对象
// 获取库对象
//MongoDatabase db = mc.getDatabase("库名");
MongoDatabase db = mc.getDatabase("myschool");
System.out.println(db);
#5.查看所有集合
//使用mongo类型的迭代器
//获取集合迭代器
MongoIterable<String> listCollectionNames = db.listCollectionNames();
//使用增强for循环遍历(也可以使用while循环)
for(String s : listCollectionNames) {
System.out.println(s);
}
#6.获取集合对象
//MongoCollection<Document> collection = db.getCollection("集合名");
MongoCollection<Document> collection = db.getCollection("student");
System.out.println(collection);
操作集合中的数据
1)新增
MongoDB的直接操作
db.student.insert({name:"张三"});
db.student.insert({name:"憨憨",
car:["BWM","大众","小鹏"]
});
db.student.insert({name:"思思",dogs:[{name:"黑豆",pz:"泰迪"},
{name:"笨笨",pz:"小土"}]
});
bson格式:{键 : 值} 其中" : " 相当于" = "
使用Java操作MongoDB
--------添加一条数据:集合对象调用insertOne()方法
传入Document对象
Document document1 = new Document();
document1.put("name", "张三");
document1.put("age", 18);
document1.put("birthday", new Date());
document1.put("sex", "男");
collection.insertOne(document1);
Document对象调用put方法,证明其本质是map,且方法没有返回值
--------添加多条数据:集合对象调用insertMany()方法
传入List集合,集合的泛型必须是Document及其子类
Document document2 = new Document();
document2.put("name", "张三");
document2.put("age", 18);
document2.put("birthday", new Date());
document2.put("sex", "男");
Document document3 = new Document();
document3.put("name", "张三");
document3.put("age", 18);
document3.put("birthday", new Date());
document3.put("sex", "男");
Document document4 = new Document();
document4.put("name", "张三");
document4.put("age", 18);
document4.put("birthday", new Date());
document4.put("sex", "男");
List<Document> listdoc = new ArrayList<Document>();
listdoc.add(document2);
listdoc.add(document3);
listdoc.add(document4);
collection.insertMany(listdoc);
2)查询
MongoDB的直接操作
db.集合名.find({查询条件});
1> 全查
db.集合名.find();
db.集合名.find({});
2> 条件查询
db.集合名.find({name:"憨憨",sex:"女"});
相当于mysql中的where name = "憨憨" and sex = "女"
db.集合名.find({age:{$lt:30}});
查询年龄<30岁的
db.集合名.find({age:{$lt:30,$gte:20}});
相当于mysql中的where age < 30 and age >= 20
$exist判断数据是否有某字段,值为boolean(true:有,false:没有)
db.集合名.find({sex:{$exist:true}});
3> 多条件查询
db.集合名.find({age:{$lt:30,$gte:20}});
同一个bson对象中写多个条件,所有的条件就会用and连接
$or关键字: 表示或者的关系
db.集合名.find({$or:[{age:{$lt:30}},
{sex:"男"}]
});
4> 模糊查询——使用正则表达式
仅包含X | /X/ | find({name:/含/}) |
以X为开头 | /^X/ | find({name:/^含/}) |
以X为结尾 | /X$/ | find({name:/含$/}) |
5> 分页查询
(页码 - 1) × 步长 = 跳过个数
db.集合名.find({查询条件}).limit(步长).skip(跳过个数);
6> 统计个数——count()
db.student.count();
db.student.find().count();
//以上两种效果相同
db.student.find({条件}).count();
db.student.count({条件});
//以上两种效果相同
7> 排序
db.student.find({条件}).sort(排序规则);
sort内为bson格式
key表示按照此字段进行排序
value:1为升序 -1为降序
使用Java操作MongoDB
需要导入Gosn驱动jar包
Gson gson = new GsonBuilder().create();
// 添加条件
Bson eq = Filters.regex("name", "张");
Document document = new Document("birthday",-1);
FindIterable<Document> find = collection.find(eq).sort(document);
for(Document doc : find) {
System.out.println(doc);
}
List<Student> slist = new ArrayList<Student>();
MongoCursor<Document> iterator = find.iterator();
while(iterator.hasNext()) {
Student s = new Student();
Document next = iterator.next();
s.setSname(next.getString("name"));
s.setSsex(next.getString("sex"));
s.setSid(next.getInteger("sid"));
// 参数1 Json 字符串
// 参数2 需要的对象的类型
// String json = next.toJson();
// System.out.println(json);
// Student s = gson.fromJson(json, Student.class);
slist.add(s);
}
for(Student ss : slist){
System.out.println(ss);
}
3)修改
MongoDB的直接操作
$set表示赋值
db.集合名.update({查询条件},{$set:{更新内容}});
$inc表示在原有基础上累加/累减任意数值(正数表示加,负数表示减)—— 传说中的点赞功能
使用Java操作MongoDB
Bson eq = Filters.eq("name", "哈哈哈");
// 多条件的
Bson and = Filters.and(Filters.gte("age", 20),Filters.lte("age", 30));
UpdateResult updateOne = collection.updateOne(eq,
new Document("$set",new Document("age", 20)));
UpdateResult updateMany = collection.updateMany(and,
new Document("$inc",new Document("age",100)));
UpdateResult updateOne = collection.updateOne(eq,
new Document("$set",new Document("age", 20)),
new UpdateOptions().upsert(true));
System.out.println(updateMany);
4)删除(危险操作)
MongoDB的直接操作
db.集合名.remove({条件}); //会删除所有符合条件的数据
db.集合名.remove(); //报错
db.集合名.remove({}); //相当于传入控制,删除所有内容
//删除一个符合条件的(删除的是第一个)
db.集合名.remove({条件},{justOne:true});
justOne的默认值为false()删除所有符合条件的,当value为false时可以省略
使用Java操作MongoDB
Bson gt = Filters.gt("age", 100);
Bson exists = Filters.exists("age");
Bson exists = Filters.exists("age", false);
DeleteResult deleteOne = collection.deleteOne(new Document("name","张三"));
DeleteResult deleteMany = collection.deleteMany(new Document("name","张三"));
DeleteResult deleteMany = collection.deleteMany(exists);
System.out.println(deleteMany);