Install MongoDB and use nosql in Windows

Source: Internet
Author: User
Tags iis download install mongodb

Http://www.xiaocai.name/emlog/post/35

MongoDB (http://www.mongodb.org) is a nosql document-type database management system, that is not a traditional relational database (similar
Oracle, MS-sqlserver, MySQL, etc ). In traditional relational databases, data is encoded in binary format and stored in tables and needs to be accessed using SQL statements.
Nosql document-based databases, such as MongoDB, are different. (Official website)

1. Install MongoDB.

Download the mongodb-win32-i386-1.4.0.zip file (Address: http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.0.zip), decompress it to the D: \ MongoDB directory. create d: \ MongoDB \ data directory (used to store database files ).

Note: The BINLOG contains the following two files: The d.exeserver and the cmd.exe client (used in PHP to connect to MongoDB for query)

Open a cmd, enter the D: \ MongoDB \ bin directory, and run the cmd.exe file. The installation is successful (O (installation □success) O) as prompted below ).

MongoDB shell version: 1.6.3
Connecting to: Test


2. Run MongoDB

First, run the server, open a cmd, and enter D: \ MongoDB \ bin \ mongod.exe -- dbpath D: \ MongoDB \ data. The following interface is displayed. (do not turn it off and register it as a system service so that it does not need to be started every time ).

Next, open a new cmd console and enter the D: \ MongoDB \ bindirectory to run the cmd.exe file.

The following code can be written)

> Show dBs; show all databases (the test database is connected by default)

Result:
Admin
Local
Test


> Show collections; displays tables in the current database

Result:

System. Indexes

System. Users

Test

User

XC



> DB. Xiaocai. insert ({Name: 'xiaoai1', age: '21'}); insert data to the Xiaocai table (if the table does not exist, it is automatically created)

> DB. Xiaocai. Find (); displays all data in the Xiaocai table

Result:

{"_ Id": objectid ("4d5cdd0fdf70000000001560"), "name": "xiaocai1", "Age": "21 "}

> DB. Xiaocai. Remove ({name: "xiaocai1"}); Delete data whose name field is 'xiaocai'

> DB. xiaocao. Find ()


Next we will focus on the Update

DB. collection. Update (criteria, objnew, upsert, multi)
Criteria: Query condition for update, similar to
Objnew: The update object and some updated operators (such as $, $ Inc...) can also be understood as
Upsert: this parameter indicates whether to insert objnew if no update record exists. True indicates insertion. The default value is false.
Multi: the default value of MongoDB is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.

DB. test0.update ({"count": {$ GT: 1 },{$ set: {"Test2": "OK"}); only the first record is updated.
DB. test0.update ({"count": {$ GT: 3 },{$ set: {"Test2": "OK" }}, false, true); all updated
DB. test0.update ({"count": {$ GT: 4 }},{ $ set: {"test5": "OK" }}, true, false); only the first entry is added.
DB. test0.update ({"count": {$ GT: 5 }},{ $ set: {"test5": "OK" }}, true, true); all added
DB. test0.update ({"count": {$ GT: 15 },{$ Inc: {"count": 1 }}, false, true); all updated
DB. test0.update ({"count": {$ GT: 10 },{ $ Inc: {"count": 1 }}, false, false); only the first entry is updated.

Corresponding SQL reference:

Select a, B from users where age = 33

DB. Users. Find ({age: 33}, {A: 1, B: 1 })

Select * from users where age> 33 and age <= 40

DB. Users. Find ({'age': {$ GT: 33, $ LTE: 40 }})

Select * from users order by name DESC

DB. Users. Find (). Sort ({Name:-1 })

Create index myindexname on users (name)

DB. Users. ensureindex ({Name: 1 })

Update users set a = 1 Where B = 'Q'

DB. Users. Update ({B: 'q'}, {$ set: {A: 1 }}, false, true)

Delete from users where z = 'abc'

DB. Users. Remove ({z: 'abc '})

3. php operations on MongoDB

Install MongoDB PHP extensions: http://github.com/mongodb/mongo-php-driver/downloads (Apache
Download V6, IIS download V9), and copy the extracted php_cmd.dll file to ext under the PHP installation directory.
Directory. Add extension = php_cmd.dll to PhP. ini. After restarting Apache, you can find the Mongo information in phpinfo.

Create a PHP file, add several new records and display them.

View plaincopy to clipboardprint?
  1. $ Conn = new Mongo ("MongoDB: // admin_miss: Miss @ localhost/test ");
  2. $ Db = $ Conn-> test; // Select Test Database
  3. $ Collection = $ db-> Xiaocai; // If the table does not exist, it is created by yourself.
  4. $ Collection-> insert (Array ('name' => 'aaa', 'age' => '21 '));
  5. $ Collection-> insert (Array ('name' => 'bbb ', 'age' => '20 '));
  6. $ Collection-> insert (Array ('name' => 'ccc ', 'age' => '25 '));
  7. $ Array = array ();
  8. Foreach ($ collection-> Find () as $ Val ){
  9. $ Array [] = $ val;
  10. }
  11. Print_r ($ array );



Update the name of the Data whose age is 20 years old to 'edit'

View plaincopy to clipboardprint?
  1. $ Collection-> Update (Array ('age' => 20), array ('$ set' => array ('name' => 'edit'), false );
  2. // Cannot write $ collection-> Update (Array ('age' => 20), array ('name' => 'edit'), false );


Delete data:

View plaincopy to clipboardprint?
  1. $ Collection-> remove (Array ('age' => 20 ));


MATERIALS:

View plaincopy to clipboardprint?
  1. // Connect to localhost: 27017
  2. $ Conn = new Mongo ();
  3. // Connect to the default port of the remote host
  4. $ Conn = new Mongo ('test. com ');
  5. // Connect to port 22011 of the remote host
  6. $ Conn = new Mongo ('test. com: 100 ');
  7. // MongoDB has a user name and password
  8. $ Conn = new Mongo ("MongoDB: // $ {username }:: {password} @ localhost ")
  9. // MongoDB has a username and password and specifies the Database Blog
  10. $ Conn = new Mongo ("MongoDB: // $ {username }:: {password} @ localhost/blog ");
  11. // Multiple servers
  12. $ Conn = new Mongo ("MongoDB: // localhost: 27017, localhost: 27018 ");


Http://hi.baidu.com/farmerluo/blog/item/9a23cb13a819bb2fdd540188.html
Http://www.searchtb.com/2010/12/a-probe-into-the-mongodb.html
Http://www.infoq.com/cn/news/2011/01/nosql-why
Bytes

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.