安装php-mongodb扩展

sudo apt-get install php-mongodb

查看扩展是否安装

php --ini

php测试文件 test_mongo.php

<?php
//链接mongo
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'百度', 'url' => 'http://www.baidu.com']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk);

$filter = ['x' => ['$gt' => 1]];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['x' => -1],
];

// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query);

foreach ($cursor as $document) {
    print_r($document);
}

运行和结果

ubuntu@VM-0-14-ubuntu:~$ php test_mongo.php 
stdClass Object
(
    [x] => 3
    [name] => taobao
    [url] => http://www.taobao.com
)
stdClass Object
(
    [x] => 2
    [name] => Google
    [url] => http://www.google.com
)

相关文章:

  • 2022-01-11
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2021-06-13
  • 2019-08-22
  • 2022-01-08
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案