PHP operation MongoDB Instance analysis, phpmongodb instance analysis
This article explains how to manipulate MongoDB in PHP. Share to everyone for your reference. The specific analysis is as follows:
MongoDB database is a database stored in JSON format, very suitable for a variety of application development, here to give friends to introduce some mongodb learning examples.
MongoDB wants 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, the example code is as follows:
Copy CodeThe code is as follows: <?php
$m = new Mongo (); This is the default connection to the local 27017 port, of course you can also connect the remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
$db = comedy, $m; Select the comedy database, if not previously the database will be automatically created, you can also use $m->selectdb ("comedy");
$collection = $db->collection; Select the collection collection inside the comedy, which is equivalent to the table inside the RDBMS, and also-can be used
$db->selectcollection ("collection");
$obj = Array ("title" = "Calvin and Hobbes", "Author" = "Bill Watterson");
$collection->insert ($obj); Adding $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 (); Disconnecting MongoDB connections
Here are some commonly used functions, the PHP code is as follows:
Copy CodeThe code is 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 ());
}
FindOne, $collection ();//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" and Index descending order
$coll->ensureindex (Array ("I" = 1, "j" + = 1)); For i "This column" indexed descending sort J Ascending
When querying, each object is automatically generated with a unique _id, which is equivalent to the primary key in the RDBMS, which is very convenient for querying, and the PHP code is as follows:
Copy CodeThe code is 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 author in comments in the following document, the PHP code is as follows:
Copy CodeThe code is as follows: {
"_id": ObjectId ("4b06c282edb87a281e09dad9"),
"Content": "This is a blog post.",
"Comments":
[
{
"Author": "Mike",
"Comment": "I think that blah blah blah ...",
},
{
"Author": "John",
"Comment": "I disagree."
}
]
}
To change the internal domain, we use the $set to ensure that the other fields in the document are not removed, and that the comment index also changes, and the PHP code is as follows:
Copy CodeThe code is as follows: <?php
$collection->update ($criteria, Array (' $set ' = = Array ("comments.1" = = Array ("Author" = "Jim"))); $criteria is the element to be updated
?>
To delete a database, the PHP code is as follows:
Copy CodeThe code is as follows: $m, dropdb ("comedy");
List all available databases with PHP code as follows:
Copy the Code code as follows: $m->listdbs (); No return value
Well, first of all, if you're interested, you can search the Internet for other uses of the Mongo-php API.
The command line uses the 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/934929.html www.bkjia.com true http://www.bkjia.com/PHPjc/934929.html techarticle PHP Operation MongoDB Instance analysis, phpmongodb example Analysis of this article describes the PHP operation MongoDB method. Share to everyone for your reference. The specific analysis is as follows: MongoDB database is a ...