Home > Backend Development > PHP7 > body text

Share a very useful php7+mongodb class!

藏色散人
Release: 2023-02-18 08:30:01
forward
1963 people have browsed it

Due to project needs, the project has been upgraded to php7. But after upgrading, I found that the mongo extension cannot be used. php7.0The above only supports mongodb extensions. The mongodb extended driver is more complicated and verbose to use than the monmgo extension. I've been looking for it online for a long time. Finally found a relatively simple mongodb class. The syntax is similar to mongo. Clear and natural.

Project addresshttps://github.com/mongodb/mongo-php-library

Because the project was contributed by foreign friends. So there is no clear document that can be read. Here are some commonly used methods.

Get instance
$uri = "mongodb://username:password@host/database";
$client = new \MongoDB\Client($uri);
Copy after login
Get collection
$collection = $client->selectCollection('test','test');
Copy after login
Get a piece of data
$data = $collection->findOne(['id'=>1]);
Copy after login
Get multiple pieces of data
$where = ['type'=>1];
$options = array(
    'projection' => array('id' => 1, 'age' => 1, 'name' => -1), // 指定返回哪些字段 1 表示返回 -1 表示不返回
    'sort' => array('id' => -1), // 指定排序字段
    'limit' => 10, // 指定返回的条数
    'skip' => 0, // 指定起始位置
);
$data = $collection->find($where,$options)->toArray();
var_dump($data);
Copy after login
Remove duplicates
$fileName = 'name';
$where = ['id' => ['$lt' => 100]]
$ret = $this->collection->distinct($fileName,$where);
Copy after login
Insert a piece of data
$data = array(
    'id' => 2,
    'age' => 20,
    'name' => '张三'
);
$ret = $collection->insertOne($data);
$id=$ret->getInsertedId();
Copy after login
Batch insert
$data = array(
    ['id' => 1, 'age' => 21, 'name' => '1xiaoli'],
    ['id' => 2, 'age' => 22, 'name' => '2xiaoli'],
    ['id' => 3, 'age' => 23, 'name' => '3xiaoli'],
    ['id' => 4, 'age' => 26, 'name' => '4xiaoli'],
    ['id' => 5, 'age' => 24, 'name' => '5xiaoli'],
    ['id' => 6, 'age' => 25, 'name' => '6xiaoli'],
);
$ret = $collection->insertMany($data);
# 返回插入id
var_dump($ret->getInsertedIds());
Copy after login
Update one item
$ret = $collection->updateOne(array('id' => 2), array('$set' => array('age' => 56)));
Copy after login
Update multiple items
$ret = $collection->updateMany(array('id' => ['$gt' => 1]), array('$set' => array('age' => 56, 'name' => 'x')));
Copy after login
Delete one item
$ret = $collection->deleteOne(array('id' => 2));
Copy after login
Delete multiple items
$collection->deleteMany(array('id' => array('$in' => array(1, 2))));
Copy after login
aggregation
$ops = [
    [
        '$match' =>['type'=>['$in'=>[2,4]]]
    ],
    [
        '$sort' => ['list.create_time' => -1]  //sort顺序不能变,否则会造成排序混乱,注意先排序再分页
    ],
    [
        '$skip' => 0
    ],
    [
        '$limit' => 20000
    ],
];
$data = $collection->aggregate($ops);
foreach ($data as $document)
{
    var_dump($document);
}
Copy after login

Recommended: "PHP7 Tutorial"

The above is the detailed content of Share a very useful php7+mongodb class!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!