(PECL mongo >= 0.8.3)
Creates a new code object
public MongoCode::__construct ( string $code [, array $scope = array() ] )
Parameters:
code
A string of code.
scope
The scope to use for the code.
Returns:
Returns a new code object.
Examples:
MongoCode::__construct() example
1 2 3 4 5 6 7 8 9 10 11 | <?php $code = new MongoCode( 'function() { ' . 'for(i=0;i<10;i++) {' . 'db.foo.update({z : i}, {z : x});' . '}' . 'return x-1;' . '}' , array ( "x" => 4)); var_dump( $code ); ?> |
The above example will output something similar to:
object(MongoCode)#1 (2) { ["scope"]=> array(1) { ["x"]=> int(4) } ["code"]=> string(80) "function() { for(i=0;i<10;i++) { db.foo.update({z : i}, {z : x}); } return x-1; }" }
Using MongoCode with $where
This example queries a collection for elements where the 'x' fields is less than $y. Notice that PHP objects can be passed into the JavaScript scope and that the JavaScript function returns a boolean.
1 2 3 4 5 | <?php $cursor = $collection ->find( array ( '$where' => new MongoCode( 'function() { return this.x < y; }' , array ( 'y' => $y )))); ?> |
Please login to continue.