Introduction to the use of MongoDB driver in PHP7

Source: Internet
Author: User
Tags mongodb driver
This article mainly introduces the introduction of the use of MongoDB driver in PHP7, has a certain reference value, now share to everyone, there is a need for friends can refer to

PHP7 can only use MongoDB driver to drive MongoDB.

Connect to a database using MongoDB driver

When I first started using MongoDB driver, I refused. View official documents only see a row of classes and unintelligible member methods, half a day can not find find, update, delete and so on keywords, more even slightly more complex such as replica set, user authentication does not know how to connect the database.
Fortunately, the document is not completely mongodb\driver\manger::constructor, click Open to see a few construction parameters

    • $uriShaped likemongo://[host:port,host:port,host:port]/[dbname]?[username=aa&password=bb&replicaSet=cc]

    • $uriOptionsThat's the last string of arguments that can be written here.

    • $driverOptionsOther options such as default read and write parameters

Mongodbdriver Curd operation

This is where I started to feel that the drive anti-human place, the document does not have any find,update,delete and other words, only one Command class and one Query类 .
The query class is a little bit better and offers some options for querying.
In fact, all operations on the MONGO database can be done through the command class. and the PHP driver documentation commandOptions is not introduced to the specific.
Just because I was MongoDB Meng new, so I will feel this drive incomparable wonderful anti-human, at all.
The key to our use of this drive is to construct a command classcommandOption
And that commandOption 's what we need to know from MongoDB's official documentation.
MONGODB Manual-database Commands
All operations, data additions and deletions, aggregation sorting, database status, user authentication and so on, can be done through these commands. Here are just a few examples.

    • Connecting to a database

$this->_conn = new Mongodb\driver\manager (' mongodb://localhost:27017/test '); $this->_db = ' test ';//Execute Command.function EXEC ($opts) {    $cmd = new Mongodb\driver\command ($opts);    $res =  $this->_conn->executecommand ($this->_db, $cmd);    return $res->toarray ();}
    • Conditional query, return on demand

$cmd = [    ' Find ' = ' table ',//collection ' filter ' = [' _id ' = ' = '    $gte ' + ', ' $lt ' and ' = ' 20]],
  
    ' projection ' = [' name ' + 1, ' email ' = 1]]; $res = $this-exec ($cmd);
  
    • Modify a record

$cmd = [    ' Update ' + ' table ',//collection '    updates ' = [        [' q '] = [_id = [' $lt ' = + 10]], ' U ' = = [' status ' + 0], ' upsert ' =>0, ' multi ' =>1],        [' q ' = = [_id] [' $gte ' +]], ' u ' = [' stat US ' + 1], ' Upsert ' =>0, ' multi ' =>1],    ],    ' ordered ' = 1,//whether the updates statement is executed sequentially, true means that after the execution fails, the following statement continues. False indicates that the failure returns immediately]
    • Delete records,limit can only be 1 or 0!

$cmd = [    ' delete ' = ' ' table ',//Collection table name    ' Deletes ' = [        [' q '] = [_id = [' $lt ' = + 5]], ' l Imit ' + 0],//0 means all, 1 means delete 1 lines]    ]
    • Inserting data

$cmd = [    ' delete ' = ' ' table ',//Collection table name    ' Deletes ' = [        [' q '] = [_id = [' $lt ' = + 5]], ' l Imit ' + 0],//0 means all, 1 means delete 1 lines]    ]
    • grouping, sorting

$cmd = [    ' aggregate ' + ' table ',//collection ' Pipeline ' and ' '        $group ' + ' class ',        ' $sor T ' = ' score '    ]
    • Read Database status

$cmd = [    ' replsetgetstatus ' + 1,]

Auxiliary class BSON

The most useful of course is the MongoDB\BSON\Javascript and MongoDB\BSON\ObjectId , respectively, the incoming JavaScript script function, and the Ojectid using MongoDB.

Exception Exception

All Mongodbdriver-generated anomalies can be MongoDB\Driver\Exception\Exception captured

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.