First knowledge of MongoDB

Source: Internet
Author: User
Tags install mongodb mongodb driver mongo shell

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.

  1. MongoDB is a separate server.
  2. MongoDB is based on documents rather than tables.
  3. A small number of schemas in MongoDB.
  4. You do not need to learn other languages.
  5. 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]
  1. > E:
  2. E: \> cd MongoDB
  3. 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]
  1. 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]
  1. E: \ mongodb>Cmd.exe
  2. MongoDB shell version: 2.0.2
  3. Connecting to: test
  4. >Show dbs; display data
  5. Admin
  6. Local
  7. >Help; view command prompts
  8. Db. help () help on db methods
  9. Db. mycoll. help () help on collection methods
  10. Rs. help () help on replica set methods
  11. Help connect ing to a db help
  12. Help admin administrative help
  13. Help misc things to know
  14. Show dbs show database names
  15. Show collections in current database
  16. Show users in current database
  17. Show profile show most recent system. profile entries wit
  18. H time>= 1 ms
  19. Use<Db_name>Set current database
  20. Db. foo. find () list objects in collection foo
  21. Db. foo. find ({a: 1}) list objects in foo whereA= 1
  22. It result of the last line evaluated; use to f
  23. Urther iterate
  24. Exit quit the mongo shell
  25. >Use testdb; switch to the testdb database. If no data exists, a new folder named testdb is created automatically after data is inserted.
  26. Switched to db testdb
  27. >Db. py. save ({a: 10}); save a message to the collection PC. If the collection does not exist, an automatically created
  28. >Db. Acc. find (); Retrieve all records
  29. {"_ Id": ObjectId ("4d32c9204e6425691e"), "a": 10}
  30. >Show collections;
  31. -
  32. System. indexes
  33. >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]
  1. <? Php
  2. // Php connection to mongoDB
  3. // Connect
  4. $ M=NewMongo ();
  5. // Select a database
  6. $ Db=$ M-> Comedy;
  7. // Select a collection (analogous to a relational database's table) to select an association (similar to a table in the associated database)
  8. $ Collection=$ Db-> Cartoons;
  9. // Add a record to add a record
  10. $ Obj=Array("Title"=>"Calvin and Hobbes","Author"=>"Bill Watterson");
  11. $ Collection-> Insert ($ Obj);
  12. // Add another record, with a different "shape" to add another record
  13. $ Obj=Array("Title"=>"XKCD","Online"=> True );
  14. $ Collection-> Insert ($ Obj);
  15. // Find everything in the collection
  16. $ Cursor=$ Collection-> Find ();
  17. // Iterate through the results output result.
  18. Foreach($ Cursor As $ Obj){
  19. Echo $ Obj["Title"]."\ N";
  20. }
  21. ?>
The input result is Calvin and HobbesXKCD, indicating that php and mongoDB are connected successfully!

The process is described in detail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.