1. Installation and use of MongoDB
1. Download MongoDB:
Download the corresponding mongodb according to your operating system version, mine is ubuntu64, so download this in /opt/: http://fastdl.mongodb.org/linux /mongodb-linux-x86_64-2.0.3.tgz
2. Unzip:
tar zxvf
/opt/mongodb-linux-x86_64-2.0.3.tgz
3. Manually create the data and log storage path of mongodb
The default path of mongodb is
/data/db/, my settings: /opt/data/mongodb/data/ and /opt/data/mongodb/log/
4. Start mongodb:
/opt/mongodb-linux-x86_64-2.0.3/bin/mongod
–dbpath /opt/data/mongodb/data/ –logpath
/opt/data/mongodb/log/m1.log –journal –fork
To start mongodb automatically when linux starts, vi /etc/rc.local
Add the above sentence
5. Operate mongodb through shell commands
the
/opt//mongodb-linux-x86_64-2.0.3/bin/mongo
> use my_mongodb // If there is no my_mongodb, it will be created automatically
This is equivalent to the database
switched to db my_mongodb
> db.user.insert({uid:1,username:”Tom”,age:25});
//If there is no user collection, automatically create and insert a piece of data, which is equivalent to writing a document to a collection
> db.user.insert({uid:2,username:”Jerry”,age:25});
the
>
In this example, 2 records are inserted into the table user of the database my_mongodb. MongoDB will implicitly create the database my_mongodb and table user, so this example does not have the process of building a database and table, you can use show
dbs and show collections to view databases and tables, as follows:
> show dbs
admin (empty)
local (empty)
my_mongodb 0.0625GB — implicitly created database
the
> show collections
system.indexes
user — Implicitly created table
the
>
6. Query records
Query all records in the table:
> db.user.find();
{ “_id” : ObjectId(“4f81a49b779282ca68fd8a59”), “uid” : 1, “username” : “Tom”, “age” : 25 }
the
{ “_id” : ObjectId(“4f81a4a1779282ca68fd8a5a”), “uid” : 2, “username” : “Jerry”, “age” : 25 }
the
>
Query the records whose username is “Jerry”:
> db.user.find({username:”Jerry”});
{ “_id” : ObjectId(“4f81a4a1779282ca68fd8a5a”), “uid” : 2, “username” : “Jerry”, “age” : 25 }
the
>
7. Modify records
Change the age of the record whose user ID is 2 to 100:
> db.user.update({uid:2},{$set:{age:100}}) ;
the
>
Check to see if it has changed:
> db.user.find({uid:2});
{ “_id” : ObjectId(“4f81a4a1779282ca68fd8a5a”), “uid” : 2, “username” : “Jerry”, “age” : 100 }
the
>
8. Delete records
Delete the record whose user ID is 1 from the table user:
> db.user.remove({uid:1});
> db.user.find();
{ “_id” : ObjectId(“4f81a4a1779282ca68fd8a5a”), “uid” : 2, “username” : “Jerry”, “age” : 100 }
the
>
It was verified that the record was indeed deleted.
2. Install the php extension of mongodb
1. If php5-dev is not installed, install it first
aptitude install php5-dev
2. Install the php extension of mongo
pecl install mongo
If you don’t know where the pecl command is, you can find it through whereis pecl first, and then run it in that directory, such as /usr/bin/pecl
install mongo
3. Configure php.ini to join
extension=mongo.so
My php.ini is under /etc/php5/apache2/
4. Restart apache
/etc/init.d/apache2 restart