略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoCursor::awaitData

2024-12-25

MongoCursor::awaitData

(PECL mongo >=1.2.11)

MongoCursor::awaitDataSets whether this cursor will wait for a while for a tailable cursor to return more data

说明

public MongoCursor::awaitData ([ bool $wait = TRUE ] ) : MongoCursor

This method is to be used with tailable cursors. If we are at the end of the data, block for a while rather than returning no data. After a timeout period, we do return as normal.

参数

wait

If the cursor should wait for more data to become available.

返回值

Returns this cursor.

错误/异常

Throws MongoCursorException if this cursor has started iterating.

范例

Example #1 MongoCursor::awaitData() example

In this example we tail the "oplog" and instead of sleeping during every iteration, we set the MongoCursor::awaitData() option. MongoCursor::hasNext() will now block until there is more data available.

<?php
$m 
= new MongoClient'mongodb://localhost:13000', array( 'replSet' => 'seta' ) );
$c $m->local->selectCollection'oplog.rs' );
$cursor $c->find( array( 'ns' => 'demo.article''op' => 'i' ) );
$cursor->tailabletrue );
$cursor->awaitDatatrue );

while (
true) {
    if (!
$cursor->hasNext()) {
        
// we've read all the results, exit
        
if ($cursor->dead()) {
            break;
        }
    } else {
        
var_dump$cursor->getNext() );
    }
}
?>

参见

MongoDB core docs on » tailable cursors.

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/mongocursor.awaitdata.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3