MongoDB is a PHP extension in the PHP driver, which is easy to install on most platforms. PHP5.1 and later versions.
1. MongoDB PHP driver
MongoDB is a PHP extension in the PHP driver, which is easy to install on most platforms. PHP5.1 and later versions.
First, check the output of phpinfo () to determine the PHP version and VC version. Here, the VC version can be simply understood as the PHP compilation mechanism. If Apache is used, VC6 is required; otherwise, VC9, such as IIS, is required. Some Zend vc8. Check whether the Thread Safety is correct. the corresponding driver package name in PHP is abbreviated as "ts ". At the same time, you must specify the value of extension_dir. all extension components are placed here.
2. install the MongoDB extension of PHP in Windows
Download the package that matches PHP, VC, and thread security on the official website, decompress the package, and move php_cmd.dll to the extension_dir directory.
If the application server (Apache, WAMPP, etc.) is started, the Mongo extension will be automatically loaded the next time PHP is started.
3. simple example of PHP driver
The Mongo class is a connection to the database. By default, the constructor tries to connect to the database server running on the local default port. The documents in MongoDB are represented by an associated array in PHP. {"foo": "bar"} corresponds to the array ("foo" => "bar") in PHP "). Arrays in MongoDB are represented as arrays in PHP. ["foo", "bar", "baz"] for arrays in PHP ("foo", "bar ", "baz "). For null, Boolean, numeric, string, and array, the PHP driver uses the PHP native type. For other types, see the manual.
Example 1:
Obtain the users set of the test Database. do not worry about whether the users set exists. if the set does not exist, the program will automatically create it in the database. As follows:
1
test->users; 6 7 ?>
Of course, you can continue to access the child set, for example, to obtain the users. addresses set:
1 $collection = $connection->users->addresses;
Note: When a MongoDB connection is created, no parameters are input in new Mongo (). Therefore, the connection is connected to the MongoDB server with localhost: 27017 by default. If you want to create a connection to a port of a host, you can write the following code:
1 $ connection = new Mongo ('example. com '); // Connect to example.com: 27017 2 $ connection = new Mongo ('example. com: 65432 '); // Connect to example.com: 65432
Some people may wonder how to connect to the server if MongoDB runs with a security authentication mechanism? You can pass in the user name and password to obtain the connection:
1 $connection = new Mongo(‘cx:123456@example:65432/test’);
Some people will notice that there is an extra "test", which is the database corresponding to the input username and password. If you do not specify the database to which the user name and password are verified, the admin database is used by default.
There are many parameters for the Mongo constructor. you can refer to the PHP Development Manual.
Example 2:
Insert a document into the users collection:
1 $user = array(“username” => “cx”, “age” => 23); 2 3 $collection->insert($user);
Note: MongoDB only accepts UTF-8 encoded data. Otherwise, an error is returned during data insertion. If the key value in the inserted document contains Chinese characters, you need to convert the Chinese characters to UTF-8 before insertion. Another solution is to use the MongoBinData class. The MongoBinData class is instantiated using a non-UTF-8 encoded string and then assigned as a value to a key. For example:
1 $ profile = array ('username' => 'foobity ', 2 3 'Pic' => new MongoBinData ('Broadway '); 4 5 );
Example 3:
Output the ID number of the first document in the users set:
1 $user = $collection->findOne(); 2 3 echo $user[‘_id’];
Example 4:
Output the names of all users:
1 $cursor = $collection->find(); 2 3 foreach ($cursor as $value) 4 5 { 7 echo $value[‘username’] . ‘
’; 9 }
Or
1 While ($cursor->hasNext()) 2 3 { 4 5 $doc = $cursor->getNext(); 6 7 echo $doc[‘username’] . ‘
’; 8 9 }
To sort the query results in ascending order by user name:
1 $cursor->sort(array(‘username’ => 1));
Note: Due to the flexible structure of the MongoDB document, make sure that each document contains the "username" key.
Example 5:
In the users set, the user's age of "lucy" is changed to 20:
1 $collection->update(array(‘username’ => ‘lucy’), array(‘$set’ : array(‘age’ : 20)));
Note: the "$ set" modifier. the default MonoDB character is "$", but "$" in PHP indicates the variable prefix. Therefore, you can use the "$ set" modifier in single quotes, you can also define it in double quotation marks, for example, "\ $ set", or in php. set mongo. pai_char = "…", You can use any character that you think can replace $.
You can use the save () method to update data. The parameter passed in by the save () method is the document to be modified. if the document already exists in the set (whether the document contains the "_ id" key), perform the update operation, otherwise, the insert operation is performed.
Example 6:
Delete the document with the username "lucy" in the users set:
1 $collection->remove(array(‘username’ => ‘lucy’));
Example 7:
Create an index for the users set:
1 $collection->