mongodb基础操作数据

public function mongodb(){
        $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
        // 插入数据
//        $bulk = new \MongoDB\Driver\BulkWrite;
//        $bulk->insert(['x' => 1, 'name'=>'孙亚楠', 'url' => 'https://www.sunyann.com']);
//        $bulk->insert(['x' => 2, 'name'=>'谷歌', 'url' => 'http://www.google.com']);
//        $bulk->insert(['x' => 3, 'name'=>'淘宝', 'url' => 'http://www.taobao.com']);
//        $manager->executeBulkWrite('mydb.sites', $bulk);

        //更新数据
//        $bulk = new \MongoDB\Driver\BulkWrite;
//        $bulk->update(
//            ['x'=>1],
//            ['$set' => ['name'=>'孙亚楠','url' => 'http://www.sunyann.com']],
//            ['multi'=>false,'upsert'=>false]
//        );
//        $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
//        $result = $manager->executeBulkWrite('mydb.sites', $bulk, $writeConcern);
        //删除
//        $bulk = new \MongoDB\Driver\BulkWrite;
//        $bulk->delete(['name'=>'淘宝'],['limit'=>0]);
//        $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
//        $res=$manager->executeBulkWrite('mydb.sites', $bulk, $writeConcern);
        // 查询数据list
        $filter = [];
        $query = new \MongoDB\Driver\Query($filter);
        $cursor = $manager->executeQuery('mydb.sites', $query);
        $arr=[];
        foreach ($cursor as $k=>$document) {
            foreach ($document as $key=>$value){
                $arr[$k][$key]=$value;
            }
        }
        dump($arr);

    }

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注