How to connect php7 with mongoDB

后端 未结 1 1724
南方客
南方客 2021-02-06 12:12

I\'m trying to connect PHP 7 with mongoDB, I installed the \"new\" MongoDB driver using pecl by following this page instructions. I can see MongoDB version 1.1.8 from phpI

相关标签:
1条回答
  • 2021-02-06 12:33

    The page that you are referring to is the low-level PHP driver for MongoDB. The API is the same as the HHVM driver for MongoDB. The documentation for both of them is the same, and can be found at http://docs.php.net/manual/en/set.mongodb.php

    The driver is written to be a bare bone layer to talk to MongoDB, and therefore misses many convenience features. Instead, these convenience methods have been split out into a layer written in PHP, the MongoDB Library. Using this library should be your preferred way of interacting with MongoDB.

    The library needs to be installed with Composer, a package manager for PHP. See also Get Composer: Installation on Linux/OSX

    For example:

    composer require "mongodb/mongodb=^1.0.0"
    

    Once you have it installed, you can try connecting using:

    <?php
     require 'vendor/autoload.php';
     $collection = (new MongoDB\Client("mongodb://127.0.0.1:27017"))->dbname->coll;
    ?>
    

    See also:

    • Doc: MongoDB PHP Library
    • MongoDB PHP Library: Getting Started
    • PHP MongoDB Driver
    0 讨论(0)
提交回复
热议问题