# 常用命令

# 数据库操作

## 登录
$ mongostat --host 10.8.0.154:27017 -u root -p eiduo521 --authenticationDatabase admin

## 切换到 admin 数据库
$ use admin

## 创建 用户名 密码
$ db.createUser({user: 'root', pwd: 'eiduo521', roles:[{role:"root",db:"admin"}]})

## 验证 
$ db.auth('root', 'eiduo521')

## 创建 用户名 密码
$ db.createUser({user: 'tk_test_read', pwd: '123456', roles: [{ role: "read", db: "ei_tk" }]})

## 验证
$ db.auth('tk_test_read', '123456')

## 配置副本集模式
$ config={"_id":"tkReplset","members":[{"_id":0,"host":"192.168.195.98:27017"},{"_id":1,"host":"192.168.195.98:27018"},{"_id":2,"host":"192.168.195.98:27019"}]};
$ rs.initiate(config);
$ rs.status();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 集合操作


## 删除集合所有记录的 sync 和 reConvertStatus 字段
$ db.getCollection("ei_question").update({}, { $unset: { sync: 1, reConvertStatus: 1 } }, { multi: true })

## 给集合的所有记录添加 qa 字段,并赋值为 0
$ db.getCollection("ei_question").update({}, {$set: {'qa': 0}}, true, true)

## 给集合中不包含 show 字段的记录添加 show 字段,并赋值为 0
$ db.getCollection("ei_question").update({'show':{$exists:false}}, {$set: {'show': 0}}, true, true)


1
2
3
4
5
6
7
8
9
10
11

# 查询

## 语法
## 同时满足多个条件:value1 < field < value
$ db.getCollection("ei_question").find({"field":{ $gt:value1, $lt: value2 })

## 判断某属性是否存在:
$ db.getCollection("ei_question").find({'properties.area' : {$exists:false}})

## 某属性存在并且是为中文:
$ db.getCollection("ei_question").find({'properties.area':{$exists:false,$regex:/[^\u4e00-\u9fa5]/}})

## 正则表达式查询:(i:忽略大小写, /^ :行开始, $/:行结束)
## /^a/, /^a./,/^a.$/	后面两种形式会扫描整个字符串,查询速度会变慢。第一种形式会在查到符合的开头后停止扫描后面的字符。
$ db.getCollection("ei_question").find({name : /acme.*corp/i })


## a的值的数量是1:(a必须是数组,一个值的情况不能算是数量为1的数组)
$ db.getCollection("ei_question").find({a:{$size:1}})

## 保留指定字段 
$ db.getCollection("ei_question").find({'source':2, 'subject':'32',  'structure.subQuestion' : { $size: 2 } }).projection({uuid:1, _id:0})

## 按类型查询
## https://docs.mongodb.com/manual/reference/operator/query/type/index.html
## 常用类型:
## Double	1	“double”
## String	2	“string”
## Object	3	“object”
## Array	4	“array”
## Binary data	5	“binData”
## ObjectId	7	“objectId”
## Boolean	8	“bool”	 
## Date	9	“date”
## Null	10	“null”
## 32-bit integer	16	“int”
## 64-bit integer	18	“long”
## Decimal128	19	“decimal”	New in version 3.4.
## Timestamp	17	“timestamp”
$ db.getCollection("ei_question").find({"show":{$type:1}})

## 按时间范围查询
$ db.getCollection("recommend_suggestion_20210224").find(
    { 
        "time" : {
            "$gte" : ISODate("2021-02-01T00:00:00.056+0000"), 
            "$lte" : ISODate("2021-03-01T00:00:00.056+0000")
        }
    }
);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# 统计

# 根据 学段 学科 分组统计
db.getCollection("ei_question").aggregate(
    [
        { 
            "$match" : { #筛选条件
                "sharingRecord.school" : {
                    "$in" : [
                        "1409", 
                        "1412"
                    ]
                }, 
                "create.time" : {
                    "$lte" : ISODate("2021-04-14T09:17:22.889+0000")
                }
            }
        }, 
        { 
            "$group" : { # 分组
                "_id" : {
                    "phase" : "$phase", 
                    "subject" : "$subject"
                }, 
                "count" : {
                    "$sum" : NumberInt(1)
                }
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 备份

mongodump -h"192.168.1.122:27017" -uroot -p eiduo521 --authenticationDatabase admin -d ei_separate --gzip --archive=./ei_separate.archive
mongodump -h"192.168.1.122:27017" -uroot -p eiduo521 --authenticationDatabase admin -d ei_tk --gzip --archive=./ei_tk.archive
1
2

# 还原

mongorestore --host 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ei_separate --gzip --archive=./ei_separate.archive
mongorestore --host 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ei_tk --gzip --archive=./ei_tk.archive
1
2
更新时间: 2021-07-16 18:56:38