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
$uri
Shaped likemongo://[host:port,host:port,host:port]/[dbname]?[username=aa&password=bb&replicaSet=cc]
$uriOptions
That's the last string of arguments that can be written here.
$driverOptions
Other 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.
$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 ();}
$cmd = [ ' Find ' = ' table ',//collection ' filter ' = [' _id ' = ' = ' $gte ' + ', ' $lt ' and ' = ' 20]],
' projection ' = [' name ' + 1, ' email ' = 1]]; $res = $this-exec ($cmd);
$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]
$cmd = [ ' delete ' = ' ' table ',//Collection table name ' Deletes ' = [ [' q '] = [_id = [' $lt ' = + 5]], ' l Imit ' + 0],//0 means all, 1 means delete 1 lines] ]
$cmd = [ ' delete ' = ' ' table ',//Collection table name ' Deletes ' = [ [' q '] = [_id = [' $lt ' = + 5]], ' l Imit ' + 0],//0 means all, 1 means delete 1 lines] ]
$cmd = [ ' aggregate ' + ' table ',//collection ' Pipeline ' and ' ' $group ' + ' class ', ' $sor T ' = ' score ' ]
$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!