PHP code example of zdz / mongodb-php7
1. Go to this page and download the library: Download zdz/mongodb-php7 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
zdz / mongodb-php7 example snippets
use \MongodbPhp7 \Connection ;
$config = [
'host' => '127.0.0.1' ,
'port' => 27017 ,
'database' => 'test' ,
'username' => '' ,
'password' => '' ,
'dsn' => '' ,
'option' => [],
'pk' => '_id' ,
'pk_convert_string' => true ,
'prefix' => '' ,
'debug' => true ,
'rows_limit' => 10
];
$db = Connection::instance($config);
$db->setConfig('database' , 'test1' );
$collection = $db->collection('first' );
$collection = $db->collection('test2.first' );
$count = $collection->insert($insertData, false );
$insertCount = $collection->insertAll([
[
'name' => '静香' ,
'age' => 10
],
[
'name' => '大熊' ,
'age' => 18
]
]);
$collection->where(['_id' => '5d71ee675c998d22b0004b92' ]);
$collection->where([
'_id' => [
'$in' => [
$db->stringConvertPk('5d71ee675c998d22b0004b92' ),
$db->stringConvertPk('5d71ee675c998d22b0004b92' )
]
]
]);
$collection->where($where)
->upsert(true )
->update([
'name' => '测试' ,
'age' => [
'$inc' => 1
]
]);
$collection->where($where)->setField($field, $value);
$collection->where($where)->setInc($field, $num);
$collection->where($where)->limit(1 )->delete();
$field = '_id,name,age' ;
$field = [
'name' ,
'age2' ,
'age2' ,
'ageSet' => [
'$add' => ['$age1' , '$age2' ]
]
];
$collection->where($where)
->sort(['age' => $db::SORT_ASC]);
$collection->where($where)
->sort(['age' => $db::SORT_ASC])
->limit(10 )
->group(['_id' => '$name' , 'age' => ['$sum' => '$age' ]])
$collection->where($where)->find();
$collection->where($where)->find($field);
$collection->where($where)->column($field);
$collection->where($where)->count();
$collection->where($where)->sample($num);
$collection->where($where)
->page($page);