I found a lot of articles on the Internet. I first searched for zf2 and mongoDB articles directly, and then I went back to the source to learn what mongoDB is and how to use it in a pure PHP environment, I also found several mongoDB zf2 modules on github and found a lot of things on the wall. After debugging, I finally got results. Below are my specific steps: 1. First download m from the official website
I found a lot of articles on the Internet. I first searched for zf2 and mongoDB articles directly, and then I went back to the source to learn what mongoDB is and how to use it in a pure PHP environment, I also found several mongoDB zf2 modules on github and found a lot of things on the wall. After debugging, I finally got results. Below are my specific steps: 1. First download m from the official website
I found a lot of articles on the Internet. I first searched for zf2 and mongoDB articles directly, and then I went back to the source to learn what mongoDB is and how to use it in a pure PHP environment, I also found several mongoDB zf2 modules on github and found a lot of things on the wall. After debugging, I finally got results. Here are my specific steps:
1. Download mongoDB from the official website, select the version corresponding to the operating system, download a zip file, decompress it, find the bin folder, and copy all the files to f: \ tools \ mongodb, create a data folder,
This is the file directory
2. Because my php version is later than 5.4, php_cmd.dll already exists in the php extension file. Therefore, you only need to modify php. ini, remove; extension = php_cmd.dll; then restart. If this dll is not available, download the latest version from https://s3.amazonaws.com/drivers.mongodb.org/php/index.html. Use the phpinfo () function to check whether mongoDb exists.
3. Open the cmd command window, cd to the above directory, and use the command
Press enter and load a lot of information. Enter "http: // localhost: 27017/" in the browser. If the following prompt appears, the installation is successful.
Note that this command window should not be closed; otherwise, the access will fail. You can refer to this address to learn the php usage of mengoDB.
4. Slave nodes
5. Find config \ application. config. php: Add 'phlymongo' to modules. Note that 'phlymongo 'must be placed before the module where you want to reference mongoDB. I spent half an hour on this issue, here I want to use it in the Book module, so I put it in front of it
5. Open config \ module. config. php under the Applicaiton module and copy the following code to it.
'mongo'=>array('server' => 'mongodb://localhost:27017','server_options' => array('connect' => true))
6. In the module where you want to reference mongoDB, for example, if I want to use mongoDB in the Book module, find Book \ module. php.
use PhlyMongo\MongoConnectionFactory;public function getServiceConfig(){ return array( 'factories' => array( 'Book\Mongo' => function ($services) { $config = $services->get('config'); $config = $config['mongo']; $factory = new MongoConnectionFactory($config['server'], $config['server_options']); return $factory->createService($services); })); }
7. You can call it in the controller. I reference it like this,
Protected $ Partition Table; protected $ _ result; public function demoAction () {$ result = $ this-> _ result; try {$ connection = $ this-> getmediaconnection (); $ database = $ connection-> selectDB ("myblogsite"); // This is a previously created database $ collection = $ database-> selectCollection ("articles "); // This Is A collection created in advance $ cursor = $ collection-> find (); while ($ cursor-> hasNext ()) {$ article = $ cursor-> getNext ();} print_r ($ article); $ connection-> close (); Exit ();} catch (\ Exception $ e) {$ result ['msg '] = $ e-> getMessage () ;}} public function getaskconnection () {if (! $ This-> Publish connection) {$ sm = $ this-> getServiceLocator (); $ this-> Publish connection = $ sm-> get ('book \ Mongo ');} return $ this-> established connection ;}
8. The final output result is as follows. The data in it is the data of my original insert