PHP Operation MongoDB instance Analysis _php skill

Source: Internet
Author: User
Tags findone mongodb

The example of this article describes the method of PHP operation MongoDB. Share to everyone for your reference. The specific analysis is as follows:

MongoDB database is a JSON format stored in the database, very suitable for a variety of application development, here to give a few friends to introduce some mongodb learning examples.

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 with the following example code:

Copy Code code as follows:
<?php
$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
$db = $m-> comedy; Select the comedy database, if the database is not previously created automatically, you can also use $m->selectdb ("comedy");
$collection = $db->collection; Select the collection set within the comedy, which is equivalent to the table in the RDBMS, also-you can use
$db->selectcollection ("collection");
$obj = Array ("title" => "Calvin and Hobbes", "Author" => "Bill Watterson");
$collection->insert ($obj); Add $obj to the $collection collection
$obj = Array ("title" => "XKCD", "online" => true);
$collection->insert ($obj);
$cursor = $collection->find ();
foreach ($cursor as $obj) {//Traverse documents in all collections
echo $obj ["title"]. "N";
}
$m->close (); Disconnect MongoDB Connection

Here are some commonly used functions, the PHP code is as follows:
Copy Code code as follows:
$query = Array ("I" => 71);
$cursor = $collection->find ($query)//Find documents that meet $query in the $collectio collection
while ($cursor->hasnext ()) {
Var_dump ($cursor->getnext ());
}

$collection-> FindOne ()//returns the first document in the $collection collection
$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

Query, each object inserts automatically generates a unique _id, which is equivalent to the primary key in the RDBMS, is very convenient for querying, the PHP code is as follows:
Copy Code code as follows:
<?php
$person = Array ("name" => "Joe");
$people->insert ($person);
$joe = $people->findone (Array ("_id" => $person [' _id ']);
?>

Update: If we want to modify the name of the author in comments in the following document, the PHP code is as follows:
Copy Code code as follows:
{
"_id": ObjectId ("4b06c282edb87a281e09dad9"),
"Content": "This is a blog post.",
"Comments":
[
{
"Author": "Mike",
"Comment": "I am so blah blah blah ...",
},
{
"Author": "John",
"Comment": "I disagree."
}
]
}

In order to change the internal domain, we use $set to ensure that the other fields in the document are not removed, and that the comment index changes, the PHP code is as follows:
Copy Code code as follows:
<?php
$collection->update ($criteria, Array (' $set ' => Array ("Comments.1" => Array ("Author" => "Jim"))); $criteria for the element to be updated
?>

To delete a database, the PHP code is as follows:
Copy Code code as follows:
$m-> dropdb ("comedy");

List all available databases, PHP code as follows:

Copy Code code as follows:
$m->listdbs (); No return value

All right, just write so much, if you are interested, you can search the Internet for other uses 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 ()

I hope this article will help you with your PHP program design.

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.