//$manager = new MongoDB\Driver\Manager("mongodb://root:Cankao2016@127.0.0.1:27017");
//$bulk = new MongoDB\Driver\BulkWrite;
// 插入数据
// $bulk->insert(['title' => '军事新闻', 'author'=>'mier', 'url' => 'http://www.runoob.com']);
// $manager->executeBulkWrite('runoob.runoob', $bulk);
//$where = ['x' => ['$lt'=>20],'name'=>'taobao'];
//查询操作
//$where = [];
//$where = ['x'=>['$gt'=>2]];
/* $options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
];*/
//$options = ['name' =>'taobao'];
// $query = new MongoDB\Driver\Query($where);
// $data = $manager->executeQuery('runoob.runoob',$query);
// print_r($data->toArray());
// foreach ($data as $document) {
// print_r($document);
// //echo "<p>".$document->_id."</p>";
// //print_r(json_decode(json_encode($document),true));
// }
/* $rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
var_dump($rp->bsonSerialize());
echo "\n", MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($rp));
*/
/*$wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY);
print_r($);
print_r($wc->bsonSerialize());
echo "\n", MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($wc));die;*/
//$wc = new MongoDB\Driver\WriteConcern(1);
//var_dump($wc->bsonSerialize());
/* $wc = new MongoDB\Driver\WriteConcern(1, 0, true);
var_dump($wc->getJournal());
$wc = new MongoDB\Driver\WriteConcern(1, 0, false);
var_dump($wc->getJournal());*/
//更新操作
/*$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(['name'=>'菜鸟教程'],['$set' =>['name'=>rand(10,10000).'abc']]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('runoob.runoob', $bulk, $writeConcern);*/
//删除操作
// $bulk = new MongoDB\Driver\BulkWrite;
// $bulk->delete(['name' => 'taobao'], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据
// // //$bulk->delete(['x' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据
// $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
// $result = $manager->executeBulkWrite('runoob.runoob', $bulk, $writeConcern);
// var_dump($result);
MongoDB CURD练习
最新推荐文章于 2022-07-03 12:10:56 发布