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
|
# 首先在本地运行MongoDB $ sudo mongod # 然后开启另一个终端进行连接 $ mongo # 首先创建一个索引,通过createdAt来进行标定,设置90秒之后自动删除 > db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 90 } ) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } # 就可以查询到里面的索引信息了 > db.log_events.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.log_events" }, { "v" : 2, "key" : { "createdAt" : 1 }, "name" : "createdAt_1", "ns" : "test.log_events", "expireAfterSeconds" : 90 } ] # 然后插入一条数据 > db.log_events.insert( { "createdAt": new Date(), "logEvent": 2, "logMessage": "Success!" } ) WriteResult({ "nInserted" : 1 })
|