MongoDB学习总结

来源:互联网 发布:鞋面设计软件 编辑:程序博客网 时间:2024/06/02 06:45

1.    启动数据库
    1.1   Windows MongoDB服务的启动/停止:net start/stop MongoDB
    1.2   mongod --dbpath数据库路径 --logpath日志文件(是文件不是路径)

备注1)服务有关的选择项:

                --install(安装服务) --reinstall(重新安装服务) --remove(删除服务)

             2) 其它选项

                 --port(端口) --bind_ip(绑定端口号)(客户端连接时需要加上服务器ip

                 --auth(连接时需要认证)

2.停止数据库 Ctrl+C
3.结构

(1)逻辑结构:文档、集合、数据库
(2)物理结构: .ns文件和数据文件(数据文件.1.0大小的两倍)

4.CRUD操作

(1) 插入insert,save

存储过程(位于system.js表中,执行:db.eval()
(2) 查询
   (1)遍历:whileforEach ,打印json:printjson
   (2)find,findOne,limit
   (3)
高级查询:
     条件查询($gt,$lt,$gte,$lte,$ne)$where(值为“this.name>1”)
     $in($nin),$all,$size(
数组元素个数)(都是针对数组)
     $exists,null值(出了真正的null字段,不存在字段也会显示)
     $mod(值为数组)
   (4)
个数count(如果前面有条件,则需要加入非零参数)

(5) skip,limit
(6)
排序:sort(1:升序,-1:降序)

(3) 修改   update

(4) 删除   remove
(5)
分组   mapreduce

5.固定集合(CappedCollection
(1)  db.createCollection("person",{capped:1,size:10000,max:1,autoIndexId:1})
   
指定max(最大文档数)必须指定size,autoIndexId作用为为_id加上索引,默认不会为固定集合的_id加上索引
(2) 不能remove,只能drop


6.大文件 (GridFS)
    (1) mongofiles put 文件名(上传文件)
    (2) mongofiles list
文件名(显示数据库中的文件)
   (3) mongofiles get
文件名(下载文件)
   (4)
文件:fs.files fs.chunks

 

7.数据导入和导出
数据导出 mongoexport -d数据库名 -c数据集名 -f字段名 -o导出文件名
数据导入 mongoimport -d数据库名 -c数据集名导出文件名
数据备份 mongodump -d 数据库名 -o 导出文件夹名
数据恢复 mongorestore -d数据库名导出文件夹名

8.用户管理
1. 添加用户:db.addUser("用户名","密码")
2. db.auth("
用户名","密码")
3.
连接:mongo -u root -p 123456

9.执行操作
1. mongo --eval "printjson(db.t1.count())"
2. mongo
文件名.js

10.索引
1.创建索引
  db.c1.ensureIndex({age:1},{background:true})
2.
索引类型
  单一索引
  组合索引
3.删除索引
  db.c1.dropIndexes()
  db.c1.dropIndexes({name:1})
4.
强制使用索引 .hint

10.慢查询日志
1.开启
(1) mongod --profile 1 --slown 200
(2) db.setProfilingLevel(2)
   db.getProfilinglevel()
2.
文件 system.profile

11.性能优化
1. 慢查询日志中如果nscanned(扫描的记录数)远大于nreturned(返回结果的记录数)的话
那么我们就要考虑通过加索引来优化记录定位了。
2. reslen如果过大,那么说明我们返回的结果集太大了,这时请查看find函数的第二个参数是否只写上了你需要的属性名。

12.状态监测
mongosniff 网络通信
mongostats 增删查改的状态
db.serverStats() 数据库实例信息
db.stats() 数据库状态信息

13.数据库架构

1.主从复制

主:--master
从:--slave --source
2.
复制集(replication set(故障自动切换)
(1) mongod --replSet rs1 --keyFile
文件名
(2) 配置初始化:_id:rs1,members:[{_id:0,host:localhost:20000,priority:1},……]
(3) rs.intiate(
配置json)

3.查看复制集状态
rs.status()
rs.isMaster()
主从配置信息:system.replset   local数据库总
4.主从操作日志oplog
oplog.rs 是一个固定长度的 capped collection,它存在于”local”数据库中
查看oplog元信息:db.printReplicationInfo()
查看slave同步状态:db.printSlaveReplicationInfo()
5.
从库可读 db.setSlaveOK()
6.
增加节点rs.add("localhost:20000")
7.
减少节点rs.remove("localhost:28014")

14.分片
(1) 分片设置

1.启动shard server:  --shardsvr
   2.
启动configa server:  --configsvr
   3.
启动route process:  mongos --configdb localhost:30000 --chunkSize1
   4.
添加shard server

db.runCommand({ addshard:"localhost:20000" })

5. 设置分片存储的数据库和数据集
db.runCommand({ enablesharding:"test" }) --设置分片存储的数据库
db.runCommand({ shardcollection: "test.users", key: { _id:1 }}) --设置分片的集合名称,且必须指定Shard Key,系统会自动创建索引

(2) 状态显示

列出所有的ShardServer: db.runCommand({ listshards: 1 })
查看Sharding信息: printShardingStatus()
判断是否是Sharding :db.runCommand({ isdbgrid:1 })
对现有的表进行Sharding: db.runCommand({ shardcollection: "test.users_2",key: { _id:1 }})
新增Shard Server :--shardsvr db.runCommand({addshard:"localhost:20002" })
移除Shard Server: db.runCommand({"removeshard" :"localhost:20002"});

(3)ReplicaSets + Sharding

db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:27017"});

 

 

 

0 0