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?
- $ Conn = new Mongo ("MongoDB: // admin_miss: Miss @ localhost/test ");
- $ Db = $ Conn-> test; // Select Test Database
- $ Collection = $ db-> Xiaocai; // If the table does not exist, it is created by yourself.
- $ Collection-> insert (Array ('name' => 'aaa', 'age' => '21 '));
- $ Collection-> insert (Array ('name' => 'bbb ', 'age' => '20 '));
- $ Collection-> insert (Array ('name' => 'ccc ', 'age' => '25 '));
- $ Array = array ();
- Foreach ($ collection-> Find () as $ Val ){
- $ Array [] = $ val;
- }
- Print_r ($ array );
Update the name of the Data whose age is 20 years old to 'edit'
View plaincopy to clipboardprint?
- $ Collection-> Update (Array ('age' => 20), array ('$ set' => array ('name' => 'edit'), false );
- // Cannot write $ collection-> Update (Array ('age' => 20), array ('name' => 'edit'), false );
Delete data:
View plaincopy to clipboardprint?
- $ Collection-> remove (Array ('age' => 20 ));
MATERIALS:
View plaincopy to clipboardprint?
- // Connect to localhost: 27017
- $ Conn = new Mongo ();
- // Connect to the default port of the remote host
- $ Conn = new Mongo ('test. com ');
- // Connect to port 22011 of the remote host
- $ Conn = new Mongo ('test. com: 100 ');
- // MongoDB has a user name and password
- $ Conn = new Mongo ("MongoDB: // $ {username }:: {password} @ localhost ")
- // MongoDB has a username and password and specifies the Database Blog
- $ Conn = new Mongo ("MongoDB: // $ {username }:: {password} @ localhost/blog ");
- // Multiple servers
- $ 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