PHP and MONGODB Operations sample detailed

Source: Internet
Author: User
Tags comments findone mongodb php and php code


MongoDB want to integrate PHP, need to install MONGO extension, this is relatively simple, now say MongoDB Phpapi and usage
Let's look at a simple example:

PHP code

1. <?php

2. $m = new Mongo (); This is the default connection to the local 27017 port, of course, you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted

3.

4. $db = $m-> comedy; Select the comedy database, if the database is not previously created automatically, you can also use $m->selectdb ("comedy");

5.

6. $collection = $db->collection; Select the collection set within the comedy, which is equivalent to the table in the RDBMS, also-you can use

7. $db->selectcollection ("collection");

8.

9. $obj = Array ("title" => "Calvin and Hobbes", "author" => "Bill Watterson");

10.

$collection->insert ($obj); Add $obj to the $collection collection

12.

13.

$obj = Array ("title" => "XKCD", "online" => true);

$collection->insert ($obj);

$cursor = $collection->find ();

17.

foreach ($cursor as $obj) {//Traverse documents in all collections

echo $obj ["title"]. "N";

20.}

21st.

22.

$m->close (); Disconnect MongoDB Connection

Some common functions are described below:

PHP code

1. $query = Array ("I" => 71);

2. $cursor = $collection->find ($query); Find documents that meet $query in the $collectio collection

3.

4. while ($cursor->hasnext ()) {

5. Var_dump ($cursor->getnext ());

6.}

7.

8. $collection-> FindOne (); Returns the first document in the $collection collection

9.

$collection-> count (); Returns the number of documents in the $collection collection

$coll->ensureindex (Array ("I" => 1)); For i "This column" indexed descending order

$coll->ensureindex (Array ("I" =>-1, "J" => 1)); For i "This column" indexed descending order of J Ascending

When you query, each object inserts a unique _id that is equivalent to the primary key in the RDBMS and is handy for querying
Such as:

PHP code

1. <?php

2.

3. $person = Array ("name" => "Joe");

4.

5. $people->insert ($person);

6.

7. $joe = $people->findone (Array ("_id" => $person [' _id ']);

8.

9.?>


When updated:

If we want to modify the name of author in comments in the following document

PHP code

1. {

2. "_id": ObjectId ("4b06c282edb87a281e09dad9"),

3. "Content": "This is a blog post.",

4. "Comments":

5. [

6. {

7. "Author": "Mike",

8. "Comment": "I am the blah blah blah ...",

9.},

10. {

One. "Author": "John",

"Comment": "I disagree."

13.}

14.]

15.}

In order to change the internal domain, we use the $set (ensure that the other fields in the document are not removed) and the comment index changes.

PHP code

1. <?php

2.

3. $collection->update ($criteria, Array (' $set ' => Array ("Comments.1" => Array ("Author" => "Jim"))); $criteria for the element to be updated

4.

5.?>


Delete a database

PHP code

1. $m-> dropdb ("comedy");

List all available databases

PHP code

1. $m->listdbs (); No return value
All right, just write so much, if you are interested, you can search the Internet for other usage of the mongo-php API!

Command line Use instance:

1. Db.system.users.find ()

2. Db.users.count ()

3. Db.users.ensureIndex ({password:-1})

4. Use test

5. Db.users.getIndexes ()

6. Db.repairdatabase ()

7. Show Users

8. Show DBS

9. Db.users.find ({username:{$in: [' 4d81a82398790 ']}}). Explain ()

Db.users.dropIndexes ()

Db.users.find (). Count ()

Db.users.find () limit (5)

Db.users.find ({"username": "SSA"})

Show collections

Db.users.remove ()

Db.user.remove ({' username ': ' admin '})

Db.user.insert ({' username ': ' admin ', ' age ': ', ' nickname ': ' admin '})

Db.user.save ({' username ': ' admin ', ' age ': ' info ': [', ' 12313 ', ' ZZSD ']})

Db.createcollection ("User")

Db.dropdatabase ()

Show collections

Db.test.drop ()

Db.copydatabase (' Test ', ' test1 ')

Show profile

Db.printcollectionstats ()

Db.adduser (' admin ', ' admin123 ')

Db.setprofilinglevel (2);

Db.setprofilinglevel (1, 10);

Db.system.profile.find ()

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.