Install and use mongodb in Windows
--- Install the mongodb Service
Download the Windows 32bit installation package
My put in e: \ mongo
Put this path e: \ mongo in our computer environment variable.
Open cmd
Cd e: \ mongo
Mkdir db
Cd bin
Mongod -- dbpath = e: \ mongo \ db
If the configuration is successfully installed, a large amount of information will be returned.
--- Install PHP mongo Extension
1. Download The php_cmd.dll extension and put it in php \ ext \
2. Modify the extension = php_cmd.dll file of php. ini.
3. Restart the server
4. Open PHPinfo. If mongo-related information is displayed, the installation is successful.
--- Operate mongodb Databases
New cmd
Cd e: \ mongo \ bin
Mongo
It is directed to the test database by default.
# Create a collection
> Db. createCollection ('person ')
> {"OK": 1}
Add, delete, modify, and query
Db. person. insert ({"name": "bobo", "age" => 23 })
Db. person. remove ({"name": "bobo "})
Db. person. update ({"name": "bobo" },{ "age" => 299 })
Db. person. find ()
We can set a user password for each database.
> Use test # specify to the test Database
> Db. addUser ('root', "root ");
> WARNING: The 'adduser' shell helper is DEPRECATED. Please use 'createuser
Ad
Successfully added user: {"user": "test", "roles": ["dbOwner"]}
--- Install a mongodb database management tool
Like phpmyadmin, the name is RockMongo.
ThinkPHP framework link mongodb
Return array (
// 'Config maps '=> 'configuration value'
'Db _ type' => 'mongo ', // Database TYPE
'Db _ host' => 'localhost', // server address
'Db _ name' => 'test', // Database NAME
'Db _ user' => 'test', // USER Name
'Db _ pwd' => 'test', // Password
'Db _ port' => '123', // PORT
);
Create a PersonModel. class. php under Home \ Model
To Inherit
<? Php
Namespace Home \ Model;
Use Think \ Model \ custom Model;
Class PersonModel extends Model
{
// It can be empty.
}
Controller instance
Public function index (){
$ Rows = 2;
$ Count = D ('person ')-> count ();
$ Page = new \ Think \ Page ($ count, $ rows );
$ All = D ('person ')-> limit ($ Page-> firstRow.', '. $ Page-> listRows)-> select ();
Print_r ($ all );
}
Output result
Array ([580f1d3957df98eef53288f6] => Array ([_ id] => 580f1d3957df98eef53288f6 [keys] => 3 [name] => bbo [age] => 27) [580f1d4857df98eef53288f7] => Array ([_ id] => 580f1d4857df98eef53288f7 [keys] => 4 [name] => bsbo [age] => 28 ))