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.

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况