Preface
I have recently studied MongoDB, mysql, MSSQL, and Oracle, but they are all SQL-based and table-based relational databases. I understand it, but I know something about it. MongoDB is based on documents. So what is MongoDB? Five Things should be learned by PHP developers.
- MongoDB is a separate server.
- MongoDB is based on documents rather than tables.
- A small number of schemas in MongoDB.
- You do not need to learn other languages.
- MongoDB has good PHP support.
1. Download MongoDB.
1. Download the corresponding operating system MongoDB. Download from the official website: http://www.mongodb.org/downloads. I am using windows 32, so download the corresponding windows 32 version. When I write this article, the latest version is 2.0.2: latest.
2. Download The MongoDB driver corresponding to PHP and use PHP to continue MongoD. Download from the official website:, (note: The vc6 code is apache server, and vc9 indicates Microsoft's IIS server. Ts indicates thread protection .)
2. Install MongoDB.
1. Create a folder named MongoDB on the edisk, for example, E: \ MongoDB,
2. decompress the downloaded MongoDB and copy all the files in the bin directory to the MongoDB directory.
3. Create the data DIRECTORY and logs directory under the MongoDB directory to store data and log files respectively.
As shown in:
4. Start mongoDB
Run the cmd command and enter:
[Plain]
- > E:
- E: \> cd MongoDB
- E: \ MongoDB> mongod -- dbpath E: \ mongoDB \ data
As shown in:
At this time, MongoDB is started, occupying port 27017.
Enter http: // localhost: 27017/in the browser. The following message is displayed:
You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
The MongoDB Database Service has been successfully started.
5. Add the mongoDB process to windows for Automatic startup, so that you do not have to perform Step 1 every time. You cannot close the command prompt.
The location where the -- dbpath database is stored can be stored in multiple locations. -- Logpath directory of the log. -- The logappend log record is appended to the old log. -- Directoryperdb allows the system to create an independent subdirectory for each database. -- ServiceName: name of the service process, -- install.
[Html]
- E: \ mongodb \>Mongod -- logpath E: \ mongodb \ logs \ mongodb. log -- logappend -- dbpath E: \ mongodb \ data -- directoryperdb -- serviceName mongodb -- install
In this way, all the log files executed will be recorded in the E: \ MangoDB \ logs \ mongodb. log file, and MongoDB will start automatically when it is started.
We can open my computer, view the system boot process, and find that mongoDB has been added to the process, you can stop and restart.
6. simple and practical commands for mongoDB.
[Html]
- E: \ mongodb>Cmd.exe
- MongoDB shell version: 2.0.2
- Connecting to: test
- >Show dbs; display data
- Admin
- Local
- >Help; view command prompts
- Db. help () help on db methods
- Db. mycoll. help () help on collection methods
- Rs. help () help on replica set methods
- Help connect ing to a db help
- Help admin administrative help
- Help misc things to know
-
- Show dbs show database names
- Show collections in current database
- Show users in current database
- Show profile show most recent system. profile entries wit
- H time>= 1 ms
- Use<Db_name>Set current database
- Db. foo. find () list objects in collection foo
- Db. foo. find ({a: 1}) list objects in foo whereA= 1
- It result of the last line evaluated; use to f
- Urther iterate
- Exit quit the mongo shell
- >Use testdb; switch to the testdb database. If no data exists, a new folder named testdb is created automatically after data is inserted.
- Switched to db testdb
- >Db. py. save ({a: 10}); save a message to the collection PC. If the collection does not exist, an automatically created
- >Db. Acc. find (); Retrieve all records
- {"_ Id": ObjectId ("4d32c9204e6425691e"), "a": 10}
- >Show collections;
- -
- System. indexes
- >Exit; exit
3. Connect MongoDB to php.
1. Copy the downloaded MongoDB driver php_mongo.dll of the php version to the ext directory of the php installation directory.
2. Open the php configuration file php. ini and add the driver: extension = php_assist.dll.
3. Restart the apache server and view phpinfo.
For example:
It indicates that mongoDB and PHP are successfully connected!
4. Use code to test mongoDB and PHP.
[Php]
- <? Php
- // Php connection to mongoDB
-
- // Connect
- $ M=NewMongo ();
-
- // Select a database
- $ Db=$ M-> Comedy;
-
- // Select a collection (analogous to a relational database's table) to select an association (similar to a table in the associated database)
- $ Collection=$ Db-> Cartoons;
-
- // Add a record to add a record
- $ Obj=Array("Title"=>"Calvin and Hobbes","Author"=>"Bill Watterson");
- $ Collection-> Insert ($ Obj);
-
- // Add another record, with a different "shape" to add another record
- $ Obj=Array("Title"=>"XKCD","Online"=> True );
- $ Collection-> Insert ($ Obj);
-
- // Find everything in the collection
- $ Cursor=$ Collection-> Find ();
-
- // Iterate through the results output result.
- Foreach($ Cursor As $ Obj){
- Echo $ Obj["Title"]."\ N";
- }
- ?>
The input result is Calvin and HobbesXKCD, indicating that php and mongoDB are connected successfully!
The process is described in detail.