【问题标题】:MongoDb- Expired Documents will not delete even with TTL IndexMongoDb-过期文档即使使用 TTL 索引也不会删除
【发布时间】:2016-03-22 18:13:48
【问题描述】:

解决方案在以下答案的上下文中,我需要删除别名 { timestamps: { createdAt: 'created_at' } }); 并只保留 { timestamps: true })

在 Gentoo Linux 上的 MongoDB 2.6.8 中,我的文档永远不会在过期后被删除。

这是一个 MongdoDb 问题,而不是 Mongoose 问题。 Mongoose 正在做它应该做的一切,因为 TTL 索引存在于我的集合中并且看起来正确。

任何建议将不胜感激

2015-11-30T12:07:24.056-0600 [TTLMonitor] Running query: query: { expireAfterSeconds: { $exists: true } } sort: {} projection: {} skip: 0 limit: 0
2015-11-30T12:07:24.056-0600 [TTLMonitor] query admin.system.indexes query: { expireAfterSeconds: { $exists: true } } planSummary: EOF ntoreturn:0 ntoskip:0 nscanned:0 nscannedObjects:0 keyUpdates:0 numYields:0 locks(micros) r:419 nreturned:0 reslen:20 0ms
2015-11-30T12:07:24.056-0600 [TTLMonitor] Running query: query: { expireAfterSeconds: { $exists: true } } sort: {} projection: {} skip: 0 limit: 0
2015-11-30T12:07:24.056-0600 [TTLMonitor] Only one plan is available; it will be run but will not be cached. query: { expireAfterSeconds: { $exists: true } } sort: {} projection: {} skip: 0 limit: 0, planSummary: COLLSCAN
2015-11-30T12:07:24.057-0600 [TTLMonitor] query database.system.indexes query: { expireAfterSeconds: { $exists: true } } planSummary: COLLSCAN ntoreturn:0 ntoskip:0 nscanned:12 nscannedObjects:12 keyUpdates:0 numYields:0 locks(micros) r:398 nreturned:4 reslen:508 0ms
2015-11-30T12:07:24.057-0600 [TTLMonitor] TTL: { createdAt: 1 }    { createdAt: { $lt: new Date(1448734044057) } }
2015-11-30T12:07:24.057-0600 [TTLMonitor] Relevant index 0 is kp: { createdAt: 1 } io: { v: 1, key: { createdAt: 1 }, name: "createdAt_1", ns: "database.cpus", expireAfterSeconds: 172800, background: true }
2015-11-30T12:07:24.057-0600 [TTLMonitor] Only one plan is available; it will be run but will not be cached. query: { createdAt: { $lt: new Date(1448734044057) } } sort: {} projection: {} skip: 0 limit: 0, planSummary: IXSCAN { createdAt: 1 }
2015-11-30T12:07:24.058-0600 [TTLMonitor]   TTL deleted: 0

one@demos ~/github/cloudimageshare-monitoring $ mongo
MongoDB shell version: 2.6.8
connecting to: test
> use database
switched to db database
> db.cpus.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "database.cpus"
    },
    {
        "v" : 1,
        "key" : {
            "createdAt" : 1
        },
        "name" : "createdAt_1",
        "ns" : "database.cpus",
        "expireAfterSeconds" : 172800,
        "background" : true
    },
    {
        "v" : 1,
        "key" : {
            "timestamp" : 1
        },
        "name" : "timestamp_1",
        "ns" : "database.cpus",
        "background" : true
    }
]
> 

> db.cpus.find()[0]
{
    "_id" : ObjectId("564561d7e97d7aa00c1b6079"),
    "updatedAt" : ISODate("2015-11-13T04:06:47Z"),
    "created_at" : ISODate("2015-11-13T04:06:47Z"),
    "timestamp" : ISODate("2015-11-13T04:06:49.423Z"),
    "avaiable" : true,
    "status" : "success",
    "metrics" : {
        "1m" : {
            "data" : 0,
            "type" : "n",
            "unit" : "unknown"
        },
        "5m" : {
            "data" : 0.01,
            "type" : "n",
            "unit" : "unknown"
        },
        "15m" : {
            "data" : 0.05,
            "type" : "n",
            "unit" : "unknown"
        }
    },
    "__v" : 0
}
> 

one@demos ~/github/cloudimageshare-monitoring/app/data $ cat models/cpu.js 
var mongoose = require('mongoose');
var CpuSchema = require("../schemas/cpu");

var Cpu = mongoose.model('Cpu', CpuSchema);
module.exports = Cpu;

one@demos ~/github/cloudimageshare-monitoring/app/data $ cat schemas/cpu.js 
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CpuSchema = new Schema({
    createdAt: { type: Date, expires: '2d' },
    timestamp : { type : Date, index: true },
    avaiable : Boolean,
    status : String,
    metrics :  { 
        '15m' : {
            data : Number,
            type : { type: String},
            unit : String
        } ,
        '5m' : {
            data : Number,
            type : { type: String},
            unit : String
        },
        '1m' : {
            data : Number,
            type : { type: String},
            unit : String
        }
    }

}, { timestamps: { createdAt: 'created_at' } });

module.exports = CpuSchema;

function saveCpu(cpuResult) {
    var cpu = new Cpu ({
        timestamp : cpuResult.timestamp,
        avaiable : cpuResult.available,
        status : cpuResult.status,
        metrics :  {
            "15m" : {
                      data : cpuResult.metrics["15m"].data,
                      type: cpuResult.metrics["15m"].type,
                      unit: cpuResult.metrics["15m"].unit
                    },
            "5m" : {
                        data : cpuResult.metrics["5m"].data,
                        type: cpuResult.metrics["5m"].type,
                        unit: cpuResult.metrics["5m"].unit
                    },
            "1m" : {
                        data : cpuResult.metrics["1m"].data,
                        type: cpuResult.metrics["1m"].type,
                        unit: cpuResult.metrics["1m"].unit
                    }
        }
    });
    cpu.save(function (err, product, numberAffected) { 
        db_finish(err, product, numberAffected, 
                  cpuResult, "cpuResult") });
}

Gentoo 中的 MongoDb 编译标志:

 - - debug       : Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful
                   backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
 - - kerberos    : Add kerberos support
 - - mms-agent   : Install the MongoDB Monitoring Service agent
 + + ssl         : Add support for Secure Socket Layer connections
 - - static-libs : Build static versions of dynamic libraries as well

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    您的 TTL 索引在 createdAt 上,而 find 结果显示您正在将时间戳保存到 created_at

    【讨论】:

    • 我认为有一些编译,其中 JSON 中的 createdAt 将是 mongodb 中的 created_at。不是这样吗?不应该是这样吗?
    • 我不认为 Mongoose 中的映射是自动的,请查看此帖子 stackoverflow.com/questions/12669615/…
    猜你喜欢
    • 2019-09-23
    • 2016-08-03
    • 2017-10-13
    • 2014-05-05
    • 2013-09-14
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多