微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的MongoDB

centos虚拟机关闭防火墙
[root@localhost /]# systemctl stop firewalld.service
[root@localhost /]# firewall-cmd --state

下载sz   上传rz  将linux服务器上的文件下载到windows上 或者 从windows上上传文件到linux上

root@river-NUC8i7HNK:~/share# sz Navicat_Premium_v12.0.26_CS_x64.exe

mv移动命令  将文件或者文件夹进行移动

mv  移动命令
drwxr-xr-x. 2 root root   231 7月   9 12:13 bin
drwxr-xr-x. 4 root root   105 7月  15 14:49 config
drwxr-xr-x. 4 root root    47 7月  15 14:35 data
-rw-r--r--. 1 root root 30608 5月  29 05:31 LICENSE-Community.txt
-rw-r--r--. 1 root root 16726 5月  29 05:31 MPL-2
-rw-r--r--. 1 root root  2601 5月  29 05:31 README
drwxr-xr-x. 4 root root    29 7月   9 16:52 shard1
drwxr-xr-x. 4 root root    29 7月   9 17:06 shard2
drwxr-xr-x. 4 root root    29 7月   9 17:18 shard3
-rw-r--r--. 1 root root 60005 5月  29 05:31 THIRD-PARTY-NOTICES
-rw-r--r--. 1 root root 81355 5月  29 05:32 THIRD-PARTY-NOTICES.gotools
[root@localhost mongodb]# 
[root@localhost mongodb]# cd /root/
[root@localhost ~]# mkdir config_2
[root@localhost ~]# mkdir shard1_2
[root@localhost ~]# mkdir shard2_2
[root@localhost ~]# mkdir shard3_2
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# mv /usr/local/mongodb/config  /root/config_2
[root@localhost ~]# mv /usr/local/mongodb/shard1  /root/shard1_2
[root@localhost ~]# mv /usr/local/mongodb/shard2  /root/shard2_2
[root@localhost ~]# mv /usr/local/mongodb/shard3  /root/shard3_2
[root@localhost ~]# 
[root@localhost ~]# cd /root/
[root@localhost ~]# ll
总用量 83016
-rw-------. 1 root root     1605 7月   9 00:49 anaconda-ks.cfg
drwxr-xr-x. 3 root root       20 7月  19 11:29 config_2
-rw-r--r--. 1 root root     1653 7月   9 00:52 initial-setup-ks.cfg
-rw-r--r--. 1 root root 84996443 7月   9 12:13 mongodb-linux-x86_64-4.0.10.tgz
drwxr-xr-x. 3 root root       20 7月  19 11:30 shard1_2
drwxr-xr-x. 3 root root       20 7月  19 11:30 shard2_2
drwxr-xr-x. 3 root root       20 7月  19 11:30 shard3_2
View Code

0:循环插入一万条数据

rs:PRIMARY> for (var i=0;i<10000;i++){
... db.test.insert({"number":i,"十倍的number":i+10})
... }
WriteResult({ "nInserted" : 1 })

1:四大命令

  db.help()                      查看数据级别的帮助

  db.mycollection.help() 查看集合级别的帮助

  rs.help()                      查看副本集的帮助

  sh.help()                     查看分片的帮助

2:常用

  1)、查看集合索引

        db.col.getIndexes()

  2)、查看集合索引大小
       db.col.totalIndexSize()

  3)删除集合所有索引
       db.col.dropIndexes()

  4)删除集合指定索引
       db.col.dropIndex("索引名称")

  5)、集合重命名 用于做备份
      > db.meterDayFreeze.renameCollection("meterDayFreeze0722")
     { "ok" : 1 }

  6) 创建索引 在指定字段上

> db.reversePowerAccurate.createIndex({"storageTiime":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.other.createIndex({"storageTiime":1})
{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
View Code

  7) 第二个参数来指定想要的键,这样既会节省传输的数据量,又会节省客户解码文档的时间和内存消耗

  db.otherData.find({},{"dataItem":"173218","_id":0})         "_id":0 :  _id列总是在查询显示 加上"_id":0 表示不显示_id列

3:副本集 常用命令

  配置mongo.conf文件

port = 27017

#数据目录
dbpath = /usr/local/mongodb/data/db

#日志所在目录
logpath = /usr/local/mongodb/data/logs/mongodb.log

#日志输出方式
logappend = true

# 在后台启动
fork=true

# 0.0.0.0表示任意IP均可连接
bind_ip=0.0.0.0

#副本集名称
replSet=rs
View Code

 

  配置好mongo.conf文件之后  第一次初始化副本集

  关联副本集各个主机
rsconf = {
...   _id: "rs",
...   members: [
...     {
...      _id: 0,
...      host: "192.168.23.130",
         "priority":2
...     },
...     {
...      _id: 1,
...      host: "192.168.23.129",
         "priority":1
...     }
...    ]
... }
  初始化副本集
> rs.initiate(rsconf)
View Code

  1)、添加一个节点

        rs.add("host:port")
  2)删除成员
       rs.remove("host:port")
  3)、查看副本集配置
       rs.config()
  4)、重新配置副本集
       rs.reconfig(config)
  5)关闭节点服务器
      rs:PRIMARY> db.shutdownServer()

  6)、替换节点成员

       cfg=rs.conf()
       cfg.members[0].host="ip:端口"
       rs.reconfig(cfg)

  7)、设置副本集节点优先级 优先级的有效取值是0~1000,可为小数,认为1
       cfg=rs.conf()
       cfg.members[2].priority=0.5
       rs.reconfig(cfg)

  8)、将节点设置为hidden节点
        cfg=rs.conf()
       cfg.members[1].priority=0
       cfg.members[1].hidden=true
       rs.reconfig(cfg)

  9)、将节点设置为延迟备份节点
        cfg=rs.conf()
        cfg.members[1].priority=0
        cfg.members[1].hidden=true
        cfg.members[1].slaveDelay=3600
        rs.reconfig(cfg)

  10) 正则 类似于模糊查询

    db.testback.find({name:{$regex:"诸葛"}})

  11) 去重 distinct 查看有多少不同的数据标识
    db.otherData.distinct("rulerCode")

  12) 聚合 根据某个字段分组求和 求平均值

                   查看集合所有文档
rs:PRIMARY> db.testback.find()
{ "_id" : ObjectId("5d39718b7932c6b6a4bf0e51"), "name" : "诸葛亮", "age" : 20 }
{ "_id" : ObjectId("5d3971987932c6b6a4bf0e52"), "name" : "张飞", "age" : 25 }
{ "_id" : ObjectId("5d3971a47932c6b6a4bf0e53"), "name" : "赵云", "age" : 20 }
{ "_id" : ObjectId("5d3971b97932c6b6a4bf0e54"), "name" : "关羽", "age" : 25 }
{ "_id" : ObjectId("5d397a807932c6b6a4bf0e55"), "name" : "jack", "age" : 99 }
{ "_id" : ObjectId("5d3a60c97932c6b6a4bf0e56"), "name" : "jack", "age" : 20 }
{ "_id" : ObjectId("5d3a60e47932c6b6a4bf0e57"), "name" : "赵云", "age" : 70 }
{ "_id" : ObjectId("5d3a627b38385de2c8d631d2"), "name" : "jack", "age" : 23 }
                     根据name分组 求和  name相同的文档个数求和
rs:PRIMARY> db.testback.aggregate({$group:{_id:"$name",total:{$sum:1}}})
{ "_id" : "jack", "total" : 3 }
{ "_id" : "诸葛亮", "total" : 1 }
{ "_id" : "张飞", "total" : 1 }
{ "_id" : "赵云", "total" : 2 }
{ "_id" : "关羽", "total" : 1 }
                    根据name分组 求age的平均值
rs:PRIMARY> db.testback.aggregate({$group:{_id:"$name",AvgAge:{$avg:"$age"}}})
{ "_id" : "jack", "AvgAge" : 47.333333333333336 }
{ "_id" : "诸葛亮", "AvgAge" : 20 }
{ "_id" : "张飞", "AvgAge" : 25 }
{ "_id" : "赵云", "AvgAge" : 45 }
{ "_id" : "关羽", "AvgAge" : 25 }
    
View Code

  13) 查看指定时间段内指定集合的文档的数量 这里是查到了66个文档

    > db.meterDayFreeze.find({"storageTime":{"$gte":"2019-07-25 00:00:00","$lte":"2019-07-25 23:59:59"}}).count()

    66

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐