第一章: MongoDB学习(一) 详细安装教程 win10
第二章:MongoDB学习(二)sql语法
第三章:MongoDB学习(三)SpringBoot整合MongoDB
第四章:MongoDB学习(四)mongodb高级查询-Query/Criteria
第五章:MongoDB学习(五)mongodb终极查询-Aggregate
连接工具可以选择上一章节中Mongodb自带的MongodbCompass。我这里采用的是Navicat 15。
MongoDB是非关系型数据库当中最像关系型数据库的,所以我们通过它与关系型数据库的对比,来了解下它的概念。
SQL概念 | MongoDB概念 | 说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 行/文档 |
column | field | 字段/域 |
index | index | 索引 |
primary key | primary key | 主键/MongoDB自动将_id字段设置为主键 |
创建数据库
使用use命令去创建数据库,当插入第一条数据时会创建数据库,例如创建一个test数据库;
> use test
switched to db test
> db.article.insert({name:"MongoDB 教程"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
删除数据库
使用db对象中的dropDatabase()方法来删除;
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
创建集合
使用db对象中的createCollection()方法来创建集合,例如创建一个student集合;
> use test
switched to db test
> db.createCollection("student")
{ "ok" : 1 }
> show collections
student
删除集合
使用collection对象的drop()方法来删除集合,例如删除一个student集合;
> db.student.drop()
true
> show collections
-- 使用collection对象的insert()方法来插入文档
db.collection.insert(document);
-- 例如:
db.student.insert([
{
'name':'小红',
'age':7,
'class':'小(二)班',
'grade':[88,99,85]
},
{
'name':'小明',
'age':8,
'class':'小(二)班',
'grade':[83,90,85]
},
{
'name':'张华',
'age':8,
'class':'小(一)班',
'grade':[90,85,90]
}
]);
-- 使用collection对象的find()方法可以获取文档
db.article.find({})
-- 查询全部
db.student.find();
-- 条件查询 class为小(二)班
db.student.find({'class':'小(二)班'});
-- 条件查询 去重
db.student.distinct('class');
-- 条件查询 age>7
db.student.find({age: {$gt: 7}});
-- 条件查询 age<8
db.student.find({age: {$lt: 8}});
-- 条件查询 age>=7
db.student.find({age: {$gte: 7}});
-- 条件查询 age<=8
db.student.find({age: {$lte: 8}});
-- 条件查询 age>=7 且 age<8
db.student.find({age: {$gte: 7, $lt: 8}});
-- 条件查询 模糊查询 name包含小
db.student.find({name: /小/});
-- 条件查询 模糊查询 name以小开头
db.student.find({name: /^小/});
db.student.find({name:{$regex:'小'}});
-- 查询指定列 (select name,age from student;)
db.student.find({},{name: 1, age: 1});
-- 排序 1升序 2降序
db.student.find().sort({age: 1});
db.student.find().sort({age: -1});
-- 分页 查询前2条
db.student.find().limit(2);
db.student.find().sort({age: -1}).limit(2);
-- 分页 跳过前2条
db.student.find().skip(2);
-- 分页 查询1-3之间的数据
db.student.find().limit(3).skip(1);
-- or查询
db.student.find({$or: [{name: /小/},{age: 8}]});
-- 查询第一条数据
db.student.findOne();
db.student.find().limit(1);
-- 查询总数
db.student.find({}).count();
-- 分组查询
db.student.aggregate([{
$match: {
age: {
$gt: 8
}
}
}, {
$group: {
"_id": {
"class": "$class",
"age": "$age"
},
"total": {
"$sum": 1
}
}
}, {
$project: {
"_id": 0,
"name": "$_id.class",
"age": "$_id.age",
"count": "$total"
}
}])
-- 通过collection对象的update()来更新集合中的文档
db.collection.update(
<query>,
<update>,
{
multi: <boolean>
}
)
# query:修改的查询条件,类似于SQL中的WHERE部分
# update:更新属性的操作符,类似与SQL中的SET部分
# multi:设置为true时会更新所有符合条件的文档,默认为false只更新找到的第一条
-- 例如 将name为小红的学生的年龄修改为9
db.student.update({'name':'小红'},{$set:{'age':9}},{multi:true});
-- save()方法可以用来替换已有文档(小红的_id为'62a154ad345f0000ab005e14')
db.student.save(
{
'_id': ObjectId('62a154ad345f0000ab005e14'),
'name':'小红',
'age':9,
'class':'小(二)班',
'grade':[88,99,85]
});
-- 通过collection对象的remove()方法来删除集合中的文档
db.collection.remove(
<query>,
{
justOne: <boolean>
}
)
# query:删除的查询条件,类似于SQL中的WHERE部分
# justOne:设置为true只删除一条记录,默认为false删除所有记录
-- 例如
db.student.remove({'name':'小红'});