MongoDB 不删除 TTL 索引文档

Posted

技术标签:

??????MongoDB ??? TTL ??????????:MongoDB not removing TTL indexed documents ??????:2016-08-03 20:31:58 ??????:

???????????????;??,(???),??????????

?? MongoDB 3.2.1?Mongoose 4.4.x??? 0.12.9 ? 4.2.6?

Mongoose ????:

var schema = new Schema(
  userId: type: Schema.Types.ObjectId, ref: 'User', required: true,
  createdAt: type: Date, required: true, default: Date.now, expires: 10
);

? Mongo Shell ? RoboMongo ???????:

????????????? TTL ??,????????????,???????????,??????,?????????,? createdAt ?????????????

???

??????:

?????1?:

?? MongoDB 3.2 ??????? sn-p ????????

var MongoClient = require('mongodb').MongoClient,
    assert = require('assert'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var url = "mongodb://localhost:27017/test"
mongoose.connect(url);

var schema = new Schema(
    userId: 
        type: Schema.Types.ObjectId,
        ref: 'User', required: true
    ,
    createdAt: 
        type: Date,
        required: true,
        default: Date.now,
        expires: 10
    
);

var User = mongoose.model('User', schema);

var user = new User( userId: new mongoose.Types.ObjectId );

user.save(function(err, doc) 
    if (err) 
        console.log(err);
    
    mongoose.disconnect();
);

????????????????(???? 10 ?)?????????? mongoose,???????

????:

Mongoose ??? 4.4.12,??????????????????? Mongo ???????????????????,??????????

以上是关于MongoDB 不删除 TTL 索引文档的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB 不删除 TTL 索引文档

mongodb 设置自动删除过期数据

MongoDB - TTL 索引 - 未删除的文档

MongoDB TTL 索引不会删除过期文档

mongodb TTL不删除文档

mongodb TTL不删除文档