MongoDB的使用

1.管理数据库:

(1).创建或切换逻辑库: use 逻辑库

(2).查看逻辑库: show dbs

(3).删除逻辑库: db.dropDatabase()

(4).创建集合: db.createCollection("集合")

2.管理集合:

1.创建集合: db.createCollection("集合")

2.查看集合: show.collections

3.删除集合: db.集合.drop()

4.查看集合记录数量: db.集合.count()

5.查看数据空间容积,单位字节 db.集合.dataSize()

6.重命名集合 db.student.renameCollection("集合新名字")

3.表达式:

(1).lt 小于 (2).gt 大于

(3).lte 小于等于 (4).gte 大于等于

(5).in 包括 (6).nin 不包括

(7).ne 不等于 (8).all 全部

(9).not 全部 (10).or 或关系

(11).$exists 含有字段

4.正则表达式:

db.student.find({name:/^元素/})

查询名字中带有所选元素的名字

db.student.find({name:/^[a-zA-Z]{2,10}$/})

查询名字有字母组成,包括大写小写字母,字母数量2-10之间,包括2和10

5.分页查询数据:

db.student.find().limit(10)

查询student集合中前10条记录,find中可以写条件

db.student.find().skip(20).limit(10)

查询student集合中数据,从第21条开始,查10条,即查询21-30条数据

6.数据排序:

sort()可以用来对结果集排序。1代表升序,-1代表降序。

db.student.find().sort({age:1})

代表对student集合中年龄大小排序,此排序为升序

db.student.find().sort({age:-1})

代表对student集合中年龄大小排序,此排序为降序

7.排除重复:

distinct()代替find()查找不重复的记录

db.student.distinct("city")

这条命令会返回一个数组,包含student集合中所有不同的城市名称。

相关推荐
阿巴~阿巴~几秒前
Ubuntu 20.04 安装 Redis
linux·服务器·数据库·redis·ubuntu
想睡hhh1 小时前
mysql表的操作——mysql表的约束
数据库·mysql
shaohaoyongchuang1 小时前
9-mysql编程
数据库
m0”-“0m1 小时前
MySQL、Nignx和Docker在Linux上的安装详解
linux·数据库·mysql
野犬寒鸦1 小时前
从零起步学习Redis || 第十章:主从复制的实现流程与常见问题处理方案深层解析
java·服务器·数据库·redis·后端·缓存
极限实验室2 小时前
Elasticsearch 备份:snapshot 镜像使用篇
数据库·elasticsearch
武子康3 小时前
Java-145 深入浅出 MongoDB 基本操作详解:数据库查看、切换、创建集合与删除完整教程
java·数据库·sql·mysql·mongodb·性能优化·系统架构
陳錄生3 小时前
ubuntu 24.10安装MongoDB
linux·mongodb·ubuntu
阿巴~阿巴~3 小时前
Centos 7/8 安装 Redis
linux·服务器·数据库·redis·centos
刘大猫.3 小时前
mysql数据库压缩
数据库·mysql·压缩·mysql数据库压缩·数据库压缩·数据库备份与压缩