This article brings you an introduction to the usage of mongodb and php (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Mognodb database connection.
Default format
Standard connection
1 | $m = new Mongo( "mongodb://${username}:${password}@localhost" );
|
Copy after login
Instance:
1 | $m = new Mongo( "mongodb://127.0.0.1:27017/admin:admin" );
|
Copy after login
The user name and password of the database are both admin
Database operation
Insert data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
$m = new Mongo( "mongodb://127.0.0.1:27017/admin:admin" );
$db = $m ->comedy;
$collection = $db ->collection;
$db ->selectCollection( "collection" );
$obj = array ( "title" => "php1" , "author" => "Bill Watterson" );
$collection ->insert( $obj );
$obj = array ( "title" => "huaibei" , "online" => true);
$collection ->insert( $obj );
$query = array ( "_id" => $obj [ '_id' ] );
$cursor = $collection ->find( $query );
foreach ( $cursor as $obj ) {
echo $obj [ "title" ] . "\n" ;
echo $obj [ "_id" ] . "\n" ;
}
$m ->close();
|
Copy after login
Conditional query
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | mysql: id = 123
mongo: array (‘id’=>123)
mysql: name link ’%bar%’
mongo: array (‘name’ => new MongoRegex(‘/.*bar.*/i’))
mysql: where id > 10
mongo: array (‘id’ => array (‘ $gt ’ => 10))
mysql: where id >= 10
mongo: array (‘id’ => array (‘ $gte ’ => 10))
mysql: where id < 10
mongo: array (‘id’ => array (‘ $lt ’ => 10))
mysql: where id <= 10
mongo: array (‘id’ => array (‘ $lte ’ => 10))
mysql: where id > 1 and id < 10
mongo: array (‘id’ => array (‘ $gt ’ => 1,’ $lt ’ => 10))
mysql: where id <> 10
mongo: array (‘id’ => array (‘ $ne ’ => 10))
mysql: where id in(123)
mongo: array (‘id’ => array (‘ $in ’ => array (1,2,3)))
mysql: where id not in(123)
mongo: array (‘id’ => array (‘ $nin ’ => array (1,2,3)))
mysql: where id = 2 or id = 9
mongo: array (‘id’ => array (‘ $or ’ => array ( array (‘id’=>2), array (‘id’=>9))))
mysql: order by name asc
mongo: array (‘sort’=> array (‘name’=>1))
mysql: order by name desc
mongo: array (‘sort’=> array (‘name’=>-1))
mysql: limit 0,2
mongo: array (‘limit’=> array (‘offset’=>0,’rows’=>2))
mysql: select name,email
mongo: array (‘name’,'email’)
mysql: select count (name)
mongo: array (‘ COUNT ’)
|
Copy after login
When querying, each Object will automatically generate a unique_ when it is inserted. id, which is equivalent to the primary key in RDBMS, is very convenient for querying (_id is different for each one, much like an automatically increased id)
1 2 3 4 5 6 | <?php
$param = array ( "name" => "joe" );
$collection ->insert( $param );
$joe = $collection ->findOne( array ( "_id" => $param [ '_id' ]));
print_R( $joe );
$m ->close();
|
Copy after login
Return result: Array ( [_id] => MongoId Object ( [$id] => 4fd30e21870da83416000002 ) [name] => joe )
Change field value
1 2 3 4 | <?php
$sign = array ( "title" => 'php1' );
$param = array ( "title" => 'php1' , 'author' => 'test' );
$joe = $collection ->update( $sign , $param );
|
Copy after login
Delete a database
List all available databases
Create a MongoDB object
1 2 3 | <?php
$mo = new Mongo();
$db = new MongoDB( $mo ,’dbname’);
|
Copy after login
Delete the current DB
1 2 3 | <?php
$db = $mo ->dbname;
$db ->drop();
|
Copy after login
Get the current database name
1 2 3 | <?php
$db = $mo ->dbname;
$db ->_tostring();
|
Copy after login
Select the desired collection:
1 2 3 4 5 6 7 8 9 10 11 12 | $mo = new Mongo();
$coll = $mo ->dbname->collname;
$db = $mo ->selectDB(’dbname’);
$coll = $db ->collname;
$db = $mo ->dbname;
$coll = $db ->collname;
$db = $mo ->dbname;
$coll = $db ->selectCollectoin(’collname’);
|
Copy after login
Insert data (MongoCollection object
1 2 3 4 | $coll = $mo ->db->foo;
$a = array (’a’=>’b’);
$options = array (’safe’=>true);
$rs = $coll ->insert( $a , $options );
|
Copy after login
Delete records in the database (MongoCollection object)
1 2 3 4 | $coll = $mo ->db->coll;
$c = array (’a’=>1,’s’=> array (’ $lt ’=>100));
$options = array (’safe’=>true);
$rs = $coll ->remove( $c , $options );
|
Copy after login
Update records in the database (MongoCollection object)
1 2 3 4 5 | $coll = $mo ->db->coll;
$c = array (’a’=>1,’s’=> array (’ $lt ’=>100));
$newobj = array (’e’=>’f’,’x’=>’y’);
$options = array (’safe’=>true,’multiple’=>true);
$rs = $coll ->remove( $c , $newobj , $options );
|
Copy after login
Query the collection to obtain a single record (MongoCollection class)
1 2 3 4 | $coll = $mo ->db->coll;
$query = array (’s’=> array (’ $lt ’=>100));
$fields = array (’a’=>true,’b’=>true);
$rs = $coll ->findOne( $query , $fields );
|
Copy after login
Query the collection to obtain multiple records (MongoCollection class)
1 2 3 4 5 6 7 8 9 10 11 | $coll = $mo ->db->coll;
$query = array (’s’=> array (’ $lt ’=>100));
$fields = array (’a’=>true,’b’=>true);
$cursor = $coll ->find( $query , $fields );
$cursor ->sort( array (‘字段’=>-1));(-1倒序,1正序)
$cursor ->skip(100);跳过100行
$cursor ->limit(100);只显示100行
返回一个游标记录对象MongoCursor。
|
Copy after login
Operations for the cursor object MongoCursor (MongoCursor class)
1 2 3 4 5 6 7 8 9 10 11 12 13 | $cursor = $coll ->find( $query , $fields );
while ( $cursor ->hasNext()){
$r = $cursor ->getNext();
var_dump( $r );
}
$cursor = $coll ->find( $query , $fields );
foreache( $cursor as $k => $v ){
var_dump( $v );
}
$cursor = $coll ->find( $query , $fields );
$array = iterator_to_array( $cursor );
|
Copy after login
This article ends here It’s all over. For more other exciting content, you can pay attention to the mongodb video tutorial column on the PHP Chinese website!
The above is the detailed content of Introduction to the usage of mongodb and php (code example). For more information, please follow other related articles on the PHP Chinese website!