博主
258
258
258
258
专辑

第三节 MongoDB的命令操作

亮子 2021-06-17 08:37:17 7476 1 0 0

1、数据库操作

MongoDB默认有三个数据库,分别是:admin、config、local,如下图:

图片alt

关于操作MongoDB的数据库,有如下命令:

# 查看所有数据库
show dbs

# 创建一个数据库
use db_hero

# 删除数据库db_hero
use db_hero
db.dropDatabase();

在执行use db_hero创建数据库,使用show dbs并不会看到新创建的数据库,MongoDB不会显示没有数据的数据库,那怎么证明数据已经存在了呢?可以使用如下命令查看:

db.stats()

效果如下:

图片alt

可以在stats中传入比例因子:KB的值为1024,MB为1024*1024,以此类推

db.stats(1024)

怎么知道我们当前操作的是哪个数据库呢?可以通过如下命令实现:

# 方法一:
db;

# 方法二:
db.getName();

2、集合的操作

1)、创建删除集合

MongoDB并不需要显性地去创建一个集合,而是在插入数据时,如果不存在就会创建。

# 创建一个集合
db.tb_hero.insert({"name":"武松","age":33,"work":"打野"});
db.tb_hero.insert({"name":"鲁智深","age":65,"work":"拔树"});
db.tb_hero.insert({"name":"武大郎","age":43,"work":"卖饼"});
db.tb_hero.insert({"name":"潘金莲","age":23,"work":"护理"});
db.tb_hero.insert({"name":"西门庆","age":36,"work":"旅游"});
db.tb_hero.insert({"name":"林冲","age":33,"work":"教头"});

# 删掉一个集合
db.tb_hero.drop()

2)、查看集合

# 查看数据库里面所有的集合

# 命令1:
show collections

# 命令2:
show tables

3)、查看集合里面的文档

## 查询文档
db.tb_hero.find()

# 条件查询
db.tb_hero.find({"age":32})
db.tb_hero.find({"age":65,"name":"鲁智深"})

# 查询前 2 条数据
db.tb_hero.find().limit(2);

# 查询最后一条
db.tb_hero.find().limit(5).skip(4)

# 查询中间两个
db.tb_hero.find().limit(2).skip(1)
db.tb_hero.find().skip(1).limit(2)

# 查询 2 条以后的数据
db.tb_hero.find().skip(2);

# 查询第一条数据
db.tb_hero.findOne();

# 查询集合记录数
db.tb_hero.find().count();

# 查询某个结果集的记录条数 统计数量
db.tb_hero.find({age: {$gte: 33}}).count();

# 如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0)
db.tb_hero.find().skip(1).limit(5).count(true);

# 或的条件查询
# 查询 age=33 或者age=23 的记录
db.tb_hero.find({$or: [{"age": 33}, {"age": 23}]})
db.tb_hero.find({$or: [{"age": 33}, {"age": 23}, {"name":"武松"}]})

# 查询 age > 32 的记录
db.tb_hero.find({age: {$gt: 32}});

# 查询 age < 50 的记录
db.tb_hero.find({age: {$lt: 50}});

# 查询 age >= 25 的记录
db.tb_hero.find({age: {$gte: 25}});

# 查询 age <= 25 的记录
db.tb_hero.find({age: {$lte: 25}});

# 查询 age >= 25 并且 age <= 40 注意书写格式
db.tb_hero.find({age: {$gte: 25, $lte: 40}});

# 模糊查询://相当于%%
db.tb_hero.find({name: /鲁/});
db.tb_hero.find({"name": /鲁/});

# 查询 name 中以 `鲁` 开头的
db.tb_hero.find({name: /^鲁/});

# 查询指定列 name、age 数据
db.tb_hero.find({}, {name: 1, age: 1});

# 查询指定列 name、age 数据, age > 25
db.tb_hero.find({age: {$gt: 25}}, {name: 1, age: 1});

# 查询 name = 武松, age = 33 的数据
db.tb_hero.find({name: "武松", age: 33});
db.tb_hero.find({name: '武松', age: 33});

# 升序:
db.tb_hero.find().sort({age: 1});

# 降序:
db.tb_hero.find().sort({age: -1});

# 按照年龄降序、名字升序
db.tb_hero.find().sort({age: -1, name: 1});

# 按照年龄降序、名字降序
db.tb_hero.find().sort({age: -1, name: -1});

# 查询去掉后的当前聚集集合中的某列的重复数据
# 会过滤掉 name 中的相同数据
# 相当于:select distict age from tb_hero;
db.tb_hero.distinct("age");

4)、修改集合中文档

# 修改记录
db.tb_hero.update({"name":"武松"},{$set:{"age":16}});
db.tb_hero.update({"name":"林冲"},{"age":16});

# 默认修改第一条符合条件的文档
db.tb_hero.update({$or: [{"age": 16}, {"age": 65}]}, {$set:{"age":18}});

# 默认修改所有符合条件的文档
db.tb_hero.update({$or: [{"age": 16}, {"age": 65}]}, {$set:{"age":18}}, {multi: true});

# 相当于:update tb_hero set age = age + 5 where name = ‘武松’;
db.tb_hero.update({name: '武松'}, {$inc: {age: 5}}, false, true);

# 相当于:update users set age = age + 5, name = ‘武松1’ where name = ‘Lisi’;
db.tb_hero.update({name: '武松'}, {$inc: {age: 5}, $set: {name: '武松1'}}, false, true);

5)、删除集合中的文档

# 删除记录
db.tb_hero.remove({age: 18});

# 删除所有记录
db.tb_hero.remove({});