Introduction of a Php7+mongodb three-party class

Source: Internet
Author: User
Tags bulk insert
This article brings the content is about a php7+mongodb three-party introduction, there is a certain reference value, the need for friends can refer to, I hope to help you.

The project was upgraded to PHP7 due to project needs. However, after upgrading, it was found that the MONGO extension was not available. php7.0 above only supports MongoDB extension. The use of MongoDB extension drivers is more complex and verbose than the Monmgo extension. On the internet for a long time. Finally found a relatively concise MongoDB class. Grammar is similar to MONGO. Clear, Natural.

Project Address Https://github.com/mongodb/mongo-php-library

Because the project is the contribution of foreign friends. So there is no clear document to see. Here are some common ways to do this.

Get instance

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

Get collection

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

Get a piece of data

$data = $collection->findone ([' ID ' =>1]);

Get more than one piece of data

$where = [' type ' =>1]; $options = array ('    projection ' = = Array (' id ' = = 1, ' age ' = 1, ' name ' = 1),//refers to  Returns which field 1 means return-1 means no return    ' sort ' = = Array (' id ' =-1),//Specifies the sort field    ' limit ' = = 10,//Specifies the number of bars returned    ' skip ' = 0,//specify starting position); $data = $collection->find ($where, $options)->toarray (); Var_dump ($data);

Go heavy

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

Insert a piece of data

$data = Array (    ' id ' = = 2, ' age ' = '    ,    ' name ' = ' Zhang San '); $ret = $collection->insertone ($data); $id = $ret->getinsertedid ();

BULK INSERT

$data = Array (    [' id ' = = 1, ' age ' = +, ' name ' = ' 1xiaoli '],    [' id ' = + 2, ' age ' = +, ' name ' = = ' 2xiaoli ',    [' id ' = + 3, ' age ' = ' + ', ' name ' = ' 3xiaoli '],    [' id ' = + 4, ' age ' = ' + ', ' name ' = ' 4xiaoli '],    [' id ' = 5, ' age ' = ' + ', ' name ' = ' 5xiaoli '],    [' id ' = + 6, ' age ' = +, ' name ' = ' 6x ' Iaoli '], $ret = $collection->insertmany ($data); # Return to insert Idvar_dump ($ret->getinsertedids ());

Update an article

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

Updating multiple lines

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

Delete a bar

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

Delete multiple strips

$collection->deletemany (Array (' id ' = = Array (' $in ' = = = Array (1, 2)));

Polymerization

$ops = [    [        ' $match ' =>[' type ' =>[' $in ' =>[2,4]]]    ,    [        ' $sort ' = = [' List.create_time ' = >-1]  //sort order can not change, otherwise it will cause sorting confusion, attention first sort and then pagination    ],    [        ' $skip ' = 0    ],    [        ' $limit ' + = 20000    ], $data = $collection->aggregate ($ops), foreach ($data as $document) {    var_dump ($document);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.