该方法给集合添加一个标识,来修改集合的行为。 标识包含usePowerOf2Sizes和index。
命令格式为:
db.runCommand({"collMod":<collection>,"<flag>":<value>})
检查标记设置:db.collection.stats()结果中的userFlags
1.通过collMod的标记修改TTL集合的过期时间:
#创建TTL集合
> db.log_events.createIndex({"createdAt": 1},{expireAfterSeconds: 360}) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.log_events.insert({ "createdAt": new Date(), "logEvent": 2, "logMessage": "Success!" }) WriteResult({ "nInserted" : 1 }) > db.log_events.find() { "_id" : ObjectId("56e4d6b51f83a0ff9e45be1c"), "createdAt" : ISODate("2016-03-13T02:55:49.002Z"), "logEvent" : 2, "logMessage" : "Success!" }
#查看TTL索引信息
> db.log_events.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.log_events" }, { "v" : 1, "key" : { "createdAt" : 1 }, "name" : "createdAt_1", "ns" : "test.log_events", "expireAfterSeconds" : 360 } ]
#使用collMod修改TTL过期时间
#格式为{keyPattern: <TTL索引>, expireAfterSeconds: <修改后的过期时间> }
> db.runCommand({collMod: 'log_events', index: {keyPattern:{createAt:1}, expireAfterSeconds:800}}) { "ok" : 0, "errmsg" : "cannot find index { createAt: 1.0 } for ns test.log_events", "code" : 72 } > db.runCommand({collMod: 'log_events', index: {keyPattern:{createdAt:1}, expireAfterSeconds:800}}) { "expireAfterSeconds_old" : 360, "expireAfterSeconds_new" : 800, "ok" : 1 } >