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.

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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊