亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    分享一個(gè)很有用的php7+mongodb類!

    由于項(xiàng)目需要,把項(xiàng)目升級(jí)到了php7。但是升級(jí)了之后發(fā)現(xiàn)mongo擴(kuò)展不能用了。php7.0以上只支持mongodb擴(kuò)展了。而mongodb擴(kuò)展的驅(qū)動(dòng)使用起來(lái)比monmgo擴(kuò)展顯得很復(fù)雜,啰嗦。在網(wǎng)上找了很久。終于找到了一個(gè)比較簡(jiǎn)潔的mongodb類。語(yǔ)法跟mongo的差不多。清晰,自然。

    項(xiàng)目地址https://github.com/mongodb/mongo-php-library

    因?yàn)轫?xiàng)目是國(guó)外友人貢獻(xiàn)的。所以沒(méi)有可以看的很明白的文檔。這里整理了一些常用的方法。

    獲取實(shí)例

    $uri = "mongodb://username:password@host/database"; $client = new MongoDBClient($uri);

    獲取集合

    $collection = $client->selectCollection('test','test');

    獲取一條數(shù)據(jù)

    $data = $collection->findOne(['id'=>1]);

    獲取多條數(shù)據(jù)

    $where = ['type'=>1]; $options = array(     'projection' => array('id' => 1, 'age' => 1, 'name' => -1), // 指定返回哪些字段 1 表示返回 -1 表示不返回     'sort' => array('id' => -1), // 指定排序字段     'limit' => 10, // 指定返回的條數(shù)     'skip' => 0, // 指定起始位置 ); $data = $collection->find($where,$options)->toArray(); var_dump($data);

    去重

    $fileName = 'name'; $where = ['id' => ['$lt' => 100]] $ret = $this->collection->distinct($fileName,$where);

    插入一條數(shù)據(jù)

    $data = array(     'id' => 2,     'age' => 20,     'name' => '張三' ); $ret = $collection->insertOne($data); $id=$ret->getInsertedId();

    批量插入

    $data = array(     ['id' => 1, 'age' => 21, 'name' => '1xiaoli'],     ['id' => 2, 'age' => 22, 'name' => '2xiaoli'],     ['id' => 3, 'age' => 23, 'name' => '3xiaoli'],     ['id' => 4, 'age' => 26, 'name' => '4xiaoli'],     ['id' => 5, 'age' => 24, 'name' => '5xiaoli'],     ['id' => 6, 'age' => 25, 'name' => '6xiaoli'], ); $ret = $collection->insertMany($data); # 返回插入id var_dump($ret->getInsertedIds());

    更新一條

    $ret = $collection->updateOne(array('id' => 2), array('$set' => array('age' => 56)));

    更新多條

    $ret = $collection->updateMany(array('id' => ['$gt' => 1]), array('$set' => array('age' => 56, 'name' => 'x')));

    刪除一條

    $ret = $collection->deleteOne(array('id' => 2));

    刪除多條

    $collection->deleteMany(array('id' => array('$in' => array(1, 2))));

    聚合

    $ops = [     [         '$match' =>['type'=>['$in'=>[2,4]]]     ],     [         '$sort' => ['list.create_time' => -1]  //sort順序不能變,否則會(huì)造成排序混亂,注意先排序再分頁(yè)     ],     [         '$skip' => 0     ],     [         '$limit' => 20000     ], ]; $data = $collection->aggregate($ops); foreach ($data as $document) {     var_dump($document); }

    推薦:《PHP7教程》

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)