-
Notifications
You must be signed in to change notification settings - Fork 0
mongo api
WinChua edited this page Jul 20, 2020
·
5 revisions
1. 创建索引
db.hello.createIndex(key, option)
e.g:
db.hello.createIndex({"key": 1}, {"expireAfterSeconds": 60}){"key": 1}, 1 表示升序, -1 表示降序;
| option | 说明 |
|---|---|
| background | 是否在后台线程中创建索引, 后台创建索引, 不会阻塞别的读写操作, 默认值是false |
| unique | 指定唯一索引, 默认为false, 为true, mongo将不会接受,重复的key |
| name | 索引名称, 如果没有指定的话, mongo将会指定一个默认的名称: ${key}_${sort_order}
|
| expireAfterSeconds | 整型值, 指定了整个文档的过期时间, key的值需要是 date类型 |
| 还有别的option暂时不知道怎么用 |
1.3 默认过期时间key
db.hello.createIndex({"createAt": 1}, {"expireAfterSeconds": 60})
db.hello.insert({"uin": 1934203186, "createAt": new Date()})
mongo文档过期删除策略: mongo会有一个后台线程, 默认是每分钟执行一次, 可能会存在某些document过期了,但是某一段时间还是生效的
db.table.stats().shards : 查看某个table下分片情况是否
sh.shardCollection("test.hello", {uin: 1}, true) : 对test.hello进行分片, 指定uin作为分片key, true表示uin是一个唯一索引;
db.table.getIndexes() : 查看table下的所有索引情况;
db.runCommand({shardcollection:"test.taginfo", unique: true, key: {uin:1} }) : 也是对某个table进行分片的操作, 需要先使用use admin命令切换到admin下