PHP operation MongoDB instance --- mongodb ttlsa tutorial series (XI)

The PHP extension installation mongodb
# https://github.com/mongodb/mongo-php-driver/archive/master.zip
# unzip master.zip
# /usr/local/php/bin/phpize
# ./config --with-php-config=/usr/local/php/bin/php-config
# make
# make install
The mongo.so added to php.ini
# vim /usr/local/php/etc/php.ini
extension = mongo.so
Restart php-fpm take effect
# /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.comf
php of mongodb expanded, provides four core class interface 1). MongoClient http://www.php.net/manual/zh/class.mongoclient.php class of operation for connecting the mongodb 2). mongodb database operations for the MongoDB http://www.php.net/manual/zh/class.mongodb.php 3). http://www.php.net/manual/zh/class.mongocollection.php 4 for a set of operations MongoCollection mongodb .) for mongodb query result set (cursor) operation based MongoCursor http://www.php.net/manual/zh/class.mongocursor.php connected drivingly connected MongoDB mongodb format: mongodb: // [username: password @ ] host1 [: port1] [, host2 [: port2], ... [, hostN [: portN]]] [/ [database]], such as [Options?]: MongoDB: // localhost MongoDB: // the User: password @ localhost mongodb: // user: password @ localhost / database mongodb: //example1.com: 27017, example2.com: 27017 mongodb: // localhost, localhost: 27018, localhost: 27019 mongodb: // host1, host2, host3 /? slaveOk = true mongodb: // localhost / safe = true mongodb: // host1, host2, host3 / safe = true; w = 2; wtimeoutMS = 2000 mongodb:?? //rs1.example.com: 27017, rs2.example .com: 27017 / replicaSet = myReplSetName mongodb :?? // localhost / journal = true & w = majority & wTimeoutMS = 20000 refer to the specific meaning of " ttlsa tutorial series mongodb- (a) mongodb introduced " http://www.ttlsa.com/html /1594.html PHP connection instance:
<?php
$m = new MongoClient("mongodb://localhost/?journal=true&w=majority&wTimeoutMS=20000");
$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName"));
$m = new MongoClient("mongodb://rs1.example.com:27017", array("replicaSet" => "myReplSetName"));
$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017", array("replicaSet" => "myReplSetName","wTimeoutMS"=>20000));
When connected to the set copy, use it to determine which station is primary. Returns the host name, port number, the health of the state (1-primary, 2-secondary, 0-other), ping time-consuming, a ping before the timestamp.
$m->getHosts();
List all the database, the database returns the name, size, and the total size is empty, ok state.
$m->listDBs();
Select the database and return the database object
$db = $m->db_name;
或
$db = $m->selectDB(db_name);
Select the table (collection), return the document collection object
$col = $m->selectCollection(db_name, col_name);
或
$col = $m->selectDB(db_name)->selectCollection(col_name);
或
$col = $m->db_name->col_name;
List all set to return a collection of objects
$col_list = $db->listCollections();
Gets the name of the currently selected database and returns the database name
$db_name = $db->__toString();
Delete the current database
$db->drop();
Slaveok set state (set state readable)
$db->setSlaveOkay(true/false);
Gets the current state of slaveok
$db->getSlaveOkay();
Inserting data MongoCollection :: insert (array $ a, array $ options) array $ a array to be inserted array $ options options: safe return operation if the result information; fsync if plugged directly into the physical hard disk; W write parts; timeout timeout time
<?php
$coll = $m->db_name->col_name;
$a = array(’website'=>’www.ttlsa.com');
$options = array(’safe’=>true);
$rs = $coll->insert($a,$options); $rs为数组,包含操作信息
Record MongoCollection delete collection :: remove (array $ criteria, array $ options) array $ criteria condition array $ options Options: safe whether to return the result of the operation; fsync whether a direct impact on the physical hard drive; whether justOne affects only a single record
<?php
$coll = $m->db_name->col_name;
$c = array(’website'=>'www.ttlsa.com',’hit’=>array(’$lt’=>100));
$options = array(’safe’=>true);
$rs = $coll->remove($c,$options); $rs为数组,包含操作信息
Record MongoCollection updated collection :: update (array $ criceria, array $ newobj, array $ options) array $ criteria array $ newobj condition to update the content of array $ options Options: safe whether to return the result of the operation; if fsync is a direct impact on physical hard disk; upsert if there is no match to add a new data; multiple affect all eligible records, the default affects only a
<?php
$coll = $m->db_name->coll_name;
$c = array(’uid'=>888,’login_count’=>array(’$lt’=>100));
$newobj = array(’vip'=>’1',’score'=>’10000');
$options = array(’safe’=>true,’multiple’=>true);
$rs = $coll->remove($c,$newobj,$options); $rs为数组,包含操作信息
Query Gets a collection of individual records MongoCollection :: findOne (array $ query, array $ fields) array $ query field conditions array $ fields to be obtained
<?php
$coll = $m->db_name->col_name;
$query = array(’score’=>array(’$lt’=>10000));
$fields = array(’uid'=>true,’vip'=>true);
$rs = $coll->findOne($query,$fields); 返回array或null
Fields taken many records MongoCollection :: find (array $ query, array $ fields) array $ query condition array $ fields to be obtained
<?php
$coll = $m->db_name->col_name;
$query = array(’s’=>array(’$lt’=>100));
$query = array(’score’=>array(’$lt’=>10000));
$fields = array(’uid'=>true,’vip'=>true);
$rs = $coll->find($query,$fields); 返回游标对象MongoCursor。
Gets the number of query results
$cursor = $coll->find();
$num = $cursor->count();
Selected Columns MongoCursor :: fields
$cursor->fields(array(column_name1 => true, column_name2 => false));
或
$cursor = $coll->find()->fields(array(column_name1 => true, column_name2 => false));
Paging
$cursor = $coll->find()->limit(30)->skip(0);
Sort MongoCursor :: sort
$cursor = $coll->find()->sort(array(column_name1 => -1, column_name2 => 1));
Take query results
$cursor = $coll->find($query,$fields);
while($cursor->hasNext()){
    $r = $cursor->getNext();
    var_dump($r);
}
或者
$cursor = $coll->find($query,$fields);
$r = array();
foreache($cursor as $k=>$v){
    var_dump($v);
    $r[] = $v;
}
或者
$cursor = $coll->find($query,$fields);
$array= iterator_to_array($cursor);
Snapshot MongoCursor :: snapshot consistency. After doing find () operation, get $ cursor, the cursor is dynamic, taking the results of the cycle process, if there are other connections to change the qualifying record, the $ cursor will follow the change. $ Cursor-> snapshot (); then, when you insert or delete records that meet the criteria to obtain the result set will not change. If the result is less than 1M rallies are automatically handled as a snapshot. If you want to get the same $ cursor after the results need to do:
$cursor = $coll->find($query,$fields);
$cursor->snapshot();
snapshot of findOne invalid. For reprint please indicate the source: PHP operation MongoDB instance  http://www.ttlsa.com/html/2288.html

Reproduced in: https: //my.oschina.net/766/blog/211153

Guess you like

Origin blog.csdn.net/weixin_33774883/article/details/91492945