Home > Backend Development > PHP Tutorial > An introduction to a php7+mongodb three-party class

An introduction to a php7+mongodb three-party class

不言
Release: 2023-04-04 06:44:02
Original
3128 people have browsed it

This article brings you an introduction to a php7 mongodb three-party class. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Due to project needs, the project has been upgraded to php7. But after upgrading, I found that the mongo extension cannot be used. PHP7.0 and above only support mongodb extension. The mongodb extension 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 that of mongo. Clear and natural.

Project address https://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

Related recommendations:

Detailed explanation of how to implement MongoDB fuzzy query in php7

##Mongodb operation class implemented by php, mongodb implemented by php

The above is the detailed content of An introduction to a php7+mongodb three-party class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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