MongoDB Installation and use

Source: Internet
Author: User
Tags mongodb server

I. MongoDB server-side installation, command-line statements

1. Go to the official website to download the latest MongoDB installation package, unzip--clip move--Rename
Cd/tmp
Curl-o http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz
Tar zxf mongodb-linux-x86_64-2.6.4.tgz
MV Mongodb-linux-x86_64-2.6.4/usr/local/mongodb

2. Install--Create a Data folder---log files and folders
Cd/usr/local/mongodb

mkdir data

mkdir logs

CD logs
Touch Db.log

Cd.. /bin

./mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs/db.log--fork


3. Edit the configuration file, join the boot boot
Vim/etc/rc.local

/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--logpath=/usr/local/mongodb/logs/db.log--fork

4. Open the MongoDB terminal
/usr/local/mongodb/bin/mongo

View Database Show DBS

Select or create a database use test

Create a new user table Db.user

Insert Data Db.user.save ({"A": 1, "B": 2});

Find the data used Db.user.find ();

View Table Show Tables | Show collections

Delete database Db.dropdatabase (); (* Enter database first, semicolon '; ')

Delete Table Db.cccc.drop (); Ibid. )


Two. MongoDB PHP extension Installation

1. Select the appropriate version of MongoDB--http://pecl.php.net/package/mongo

Cd/tmp
Curl-o http://pecl.php.net/get/mongo-1.4.1.tgz
Tar zxf mongo-1.4.1.tgz
CD MONGO-1.4.1/

2. Compile--need to know phpize and Php-config Road strength
Whereis phpize
Whereis Php-config

/usr/bin/phpize

Ls

./configure--with-php-config=/usr/bin/php-config

Make && make install

Cd/usr/lib64/php/modules

Ls

Vim/etc/php.ini

Extension = mongo.so

Restart httpd

Three. PHP Operation MongoDB

$conn =new Mongo ();//The simplest connection
$db = $conn->test;//Select or create test database
$collection = $db->user;//Select table

New
$obj = Array ("title" = "java". Rand (1,1111), "Author" = "Bill Watterson", "Age" =>mt_rand (1,100));
$collection->insert ($obj);
echo $obj [' _id ']. ' <br> ';

Delete Collection
$collection->remove ();

Modify
$where = Array (' Author ' = ' Bill Watterson ');
$newdata = Array (' $set ' =>array (' title ' = ' ngix1111 '));
$upsert = Array (' Upsert ' =>false); If no modified data is present, the default is false to insert this data
$multi = Array (' multiple ' =>true); Whether to modify all matching records, default false to modify only one
$collection->update ($where, $newdata, $multi);

Inquire
$collection->count ();//Total

Note: $GT is greater than, $GTE is greater than or equal, $lt is less than, $lte is less than or equal, $ne is not equal, $exists does not exist
$where = Array (' Age ' =>array (' $gt ' =>10, ' $lte ' =>40));
echo $collection->count ($where);

$query = Array (' Age ' =>array (' $gt ' =>60)); Query criteria
$fields = Array (' _id ' =>false, ' author ' =>false);//Display field
$cursor = $collection->find ();//Get All records
$cursor = $collection->find ($query, $fields);

Sort
$cursor->sort (Array (' age ' =>-1);//(-1 reverse, 1 positive order)
Show only partial records
$cursor->limit (4);//Display only 100 rows

foreach traversal
foreach ($cursor as $key = = $value) {
echo "<pre>";
Print_r ($value);
echo "</pre>";
}

While traversal
/*
while ($cursor->hasnext ()) {
$r = $cursor->getnext ();
echo "<pre>";
Print_r ($R);
echo "</pre>";
}
*/

$array = Iterator_to_array ($cursor);//array key name is $id
$conn->close ();

MongoDB installation and use

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.