Making a Connection
To connect to the database server, use one of the following:
<?php
$connection = new MongoClient(); // connects to localhost:27017
$connection = new MongoClient( "mongodb://example.com" ); // connect to a remote host (default port: 27017)
$connection = new MongoClient( "mongodb://example.com:65432" ); // connect to a remote host at a given port
?>
You do not have to explicitly disconnect from the database. The driver uses persistent connections and will re-use already established connections.
See Also
The chapter on connecting covers different types of connections.
The API documentation on the MongoClient class and MongoClient::__construct() give a comprehensive look at all possible options with a number of examples.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Manual
- Making a Connection
- Getting a Database
- Getting A Collection
- Inserting a Document
- Finding Documents using MongoCollection::findOne
- Adding Multiple Documents
- Counting Documents in A Collection
- Using a Cursor to Get All of the Documents
- Setting Criteria for a Query
- Getting A Set of Documents With a Query
- Creating An Index
Коментарии
Another way to connect, that works for me:
require 'vendor/autoload.php';//composer require "mongodb/mongodb=^1.0.0.0"
$m = new MongoDB\Client("mongodb://alex:mypassword@10.111.0.2:27017/");
echo "Connection to database successfull. <br>";
The Class has changed again :
$m = new MongoDB\Driver\Manager("mongodb://alex:mypassword@10.111.0.2:27017/");