MongoDB::execute

(PECL mongo >=0.9.3)

MongoDB::execute锟斤拷锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟絁avaScript

说锟斤拷

public MongoDB::execute ( mixed $code [, array $args = array() ] ) : array

MongoDB锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷JavaScript锟斤拷锟芥。锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诜锟斤拷锟斤拷锟斤拷锟街达拷锟斤拷锟斤拷锟絁avaScript锟斤拷锟诫。锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷媒锟斤拷锟斤拷锟皆达拷锟斤拷锟斤拷锟斤拷锟斤拷锟较o拷锟斤拷锟斤拷锟节凤拷锟斤拷锟斤拷锟较达拷锟斤拷一些锟斤拷锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷绱拷洌拷锟矫达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫★拷

锟节凤拷锟斤拷锟斤拷锟斤拷锟斤拷JavaScript锟斤拷锟斤拷岽达拷锟揭伙拷锟叫达拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街达拷小锟斤拷锟斤拷锟斤拷锟揭伙拷魏锟绞憋拷铣锟斤拷拇锟斤拷锟街帮拷锟斤拷肟硷拷堑锟斤拷锟揭伙拷恪�

锟斤拷锟斤拷一锟斤拷锟斤拷锟捷匡拷指锟斤拷陌锟阶帮拷锟斤拷锟斤拷虻サ锟剿碉拷嗟憋拷冢锟�

<?php

public function execute($code$args) {
    return 
$this->command(array('$eval' => $code'args' => $args));
}

?>

锟斤拷锟斤拷锟街达拷械拇锟斤拷锟街伙拷锟揭伙拷锟斤拷锟戒,锟斤拷只锟斤拷一锟叫o拷MongoDB锟斤拷锟斤拷一锟斤拷return锟斤拷洹o拷锟斤拷锟斤拷锟揭恍┲憋拷鄣锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接凤拷锟斤拷"foo"锟斤拷

<?php

$db
->execute('"foo";');

?>

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接凤拷锟斤拷NULL:

<?php

$db
->execute('"bar"; "foo";'); // 锟斤拷锟斤拷锟斤拷

$db->execute('db.foo.count(
);'
); // 锟斤拷锟斤拷

?>

为锟剿凤拷止锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟矫诧拷要锟斤拷锟斤拷MongoDB锟斤拷锟斤拷锟斤拷姆锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟饺凤拷锟斤拷峁┮伙拷锟絩eturn锟斤拷洹o拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫o拷锟斤拷锟皆把达拷锟斤拷锟轿拷锟�

<?php

$db
->execute('"bar"; return "foo";');

$db->execute('return db.foo.count(
);'
);

?>

锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟结返锟斤拷"foo"锟斤拷锟节讹拷锟斤拷锟斤拷锟结返锟斤拷"foo"锟斤拷锟较的筹拷锟饺★拷

锟斤拷锟斤拷

code

MongoCode锟斤拷要执锟叫碉拷锟街凤拷锟斤拷

args

锟斤拷code锟侥诧拷锟斤拷锟斤拷

锟斤拷锟斤拷值

锟斤拷锟斤拷执锟叫斤拷锟�

锟斤拷锟斤拷

Example #1 锟津单碉拷 MongoDB::execute() 实锟斤拷

<?php

$response 
$db->execute("function() { return 'Hello, world!'; }");
echo 
$response['retval'];

?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�


Hello, world!

Example #2 锟斤拷锟斤拷锟斤拷锟斤拷 MongoDB::execute() 实锟斤拷

锟斤拷选锟侥诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷革拷JavaScrip锟斤拷锟斤拷

<?php

$response 
$db->execute("function(greeting, name) { return greeting+', '+name+'!'; }", array("Good bye""Joe"));
echo 
$response['retval'];

?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�


Good bye, Joe!

Example #3 锟斤拷锟斤拷锟斤拷实锟斤拷

锟斤拷锟绞癸拷锟� MongoCode 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟轿拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷虻浇锟揭达拷械锟絁avaScript锟叫★拷

<?php

$func 

    
"function(greeting, name) { ".
        
"return greeting+', '+name+', says '+greeter;".
    
"}";
$scope = array("greeter" => "Fred");

$code = new MongoCode($func$scope);

$response $db->execute($code, array("Goodbye""Joe"));
echo 
$response['retval'];

?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�


Goodbye, Joe, says Fred