dsgwdigu84950 2014-06-26 15:58
浏览 85
已采纳

使用php在mongodb中的某个时钟时间到期文档

I have this code inside my php:

    $m = new MongoClient();
    $db = $m->selectDB('authentication');
    $collection = new MongoCollection($db, 'digits');
    $document = array( 
        "username" => $_POST['username'], 
        "digits" => $_POST['digits']
        );
    $collection->insert($document);

I want these documents to be deleted after 2 hours automatically using ttl feature of mongodb. there may be thousands of documents inserting every minute, so I don't want them to get messy or buggy, I want them to be deleted independently in the same collection. If you can put the code in php I'd appreciate it. because everywhere else they just explained mongodb commands directly, which I couldn't understand how to use it in php. thanks.

Edit 1: with the help of "Christian P", I created 30 documents for test:

    for($i=0;$i<30;$i++){
        $m = new MongoClient();
        $db = $m->selectDB('authentication');
        $collection = new MongoCollection($db, 'teeeest');
        $collection->ensureIndex(array('createdAt' => 1, 'expireAfterSeconds' => 60));
        $document = array( 
                    "username" => "4563678678", 
                    "digits" => "5958974",
                    "createdAt" => new MongoDate()
                    );
        $collection->insert($document);

        sleep(1);
    }

but they are not being removed. an example of created documents:

{
    "_id": {
        "$oid": "53ac7c237fae31100e000109"
    },
    "username": "4563678678",
    "digits": "5958974",
    "createdAt": {
        "$date": "2014-06-26T20:01:39.000Z"
    }
}

{
    "_id": {
        "$oid": "53ac7c247fae31100e00010a"
    },
    "username": "4563678678",
    "digits": "5958974",
    "createdAt": {
        "$date": "2014-06-26T20:01:40.000Z"
    }
}

{
    "_id": {
        "$oid": "53ac7c257fae31100e00010b"
    },
    "username": "4563678678",
    "digits": "5958974",
    "createdAt": {
        "$date": "2014-06-26T20:01:41.000Z"
    }
}

Edit 2: as "Christian P" said in his edit, "expireAfterSeconds" should be passed as an array.

  • 写回答

1条回答 默认 最新

  • dongshixingga7900 2014-06-26 17:55
    关注

    To automatically expire data from collection by setting TTL you must do two things:

    1. Create a Date field.
    2. Create a TTL index on that field.

    To create a Date field in PHP you need to use MongoDate object:

    $document = [ 
        "username" => $_POST['username'], 
        "digits" => $_POST['digits'],
        "createdAt" => new MongoDate()
    ];
    

    You can add a TTL index using ensureIndex command, as other regular indexes.

    $collection->ensureIndex(
        ['createdAt' => 1], ['expireAfterSeconds' => 7200]
    );
    

    The above command will add an index TTL index on the createdAt that will delete documents after 2 hours.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化