北京网帮你
芒果数据库mongoDB 创建数据库与操作
时间:2019-11-04 23:56:55 浏览:208

创建数据库

use databaseName

e.g.  use stu  #创建一个stu数据库

* use 实际上是表示选择某个数据库使用。当这个数据库不存在时会自动创建。
* 使用use后数据不会马上被创建,而是在实际写入数据时才会创建


查看当前系统中数据库
show  dbs

系统数据库:
admin:存放用户及其权限
local: 存储本地数据
config:存储分片信息

数据库的命名规则
1. 使用utf-8字符
2. 不能有空格,点,/ \  '\0'字符
3. 长度不超过64字节
4. 不和系统数据库重名

db:mongodb系统全局变量,代表当前正在使用的数据库

* 如果没有use任何数据库情况下 db表示test。此时插入数据则创建test数据库


数据库备份和恢复

备份:mongodump -h dbhost -d    dbname   -o dbdir
                  主机      要备份数据库   目录

e.g.  
将stu数据库备份到student目录中
mongodump -h  127.0.0.1 -d stu -o student

恢复:mongorestore -h  dbhost:port -d dbname  path
                      主机           数据库  目录

e.g.
将stu数据库恢复到student数据库中
mongorestore -h 127.0.0.1:27017 -d student  student/stu

数据库的监测
mongostat

insert query update delete: 每秒执行增删改查次数
command : 每秒运行命令次数
flushes : 每秒清理缓存次数
vsize :使用的虚拟内存
res:物理内存

mongotop
监测每个数据库的读写时长

ns         total     read       write
数据集合    总时长    读时长    写时长


删除数据库
db.dropDatabase()

删除db代表的数据库

创建集合

db.createCollection(collection_name)

e.g.  
创建一个class1的集合
db.createCollection("class1")

创建集合2
当向一个集合中插入数据的时候,如果这个集合不存在则会自动创建

db.collecionName.insert(...)

e.g.  如果class2不存在则自动创建    db.class2.insert({"name":'Tom','age':17,'sex':'m'})

查看数据库中集合
show collections
show tables

集合命名规则
1. utf-8 字符
2. 不能有'\0'
3. 不要以system.开头,因为这是系统保留集合前缀
4. 不要和关键字重复


删除集合
db.collectionName.drop()

e.g.   db.class.drop()   #删除class这个集合

集合的重命名

db.collectionName.renameCollection("new_name")

e.g.  将class2重命名为class0
db.class2.renameCollection("class0")


[上一篇]芒果数据库mongodb中数据的组织形
[下一篇]芒果数据库mongoDB 安装说明
Copyright 2022 © 93580.com.cn 网帮你

2022 © 版权所有 红花岗区网帮你信息技术工作室

工信部备案号:黔ICP备2024036985号-2

请使用网帮你微信扫码登录