PHP Operations MongoDB

Source: Internet
Author: User
Tags mongodb

one, linked database


$conn = new Mongo ("Mongodb://ip:port/dbname:dbpwd");
This is the default connection to the local 27017 port, of course, you can also connect remote host such as $db = $conn->selectdb ("dbname"); $collection = $db->selectcollection (' tablename '); This is called TableName easy to understand, in fact MONGO is called set $dbs = $conn->listdbs ();
Gets an array//var_dump ($DBS) containing the DB information; $tbs = $db->listcollections ()//Get an array that contains the collection ($tbs as $val) {echo "TB1: $val <br>";} echo "Count:". $
Collection->count ();
echo "<br>";
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
$cursor = $collection->find ();
$rs = $cursor->sort (Array ("key" =>1))->skip->limit;//skip equivalent to SQL Start,limit equivalent to MySQL offset echo "<table><tr><td align= ' center ' >id</td><td align= ' center ' >width</td>< TD align= ' Center ' >height</td><td align= ' center ' >url</td><td align= ' center ' >from</td
&GT;&LT;TD align= ' center ' >key</td><td align= ' center ' >title</td></tr> '; foreach ($rs as $val) {//var_dUMP ($val); echo "<tr><td>". $val ["_id"]. " </td><td> ". $val [" width "]." </td><td> ". $val [" height "]." </td><td> ". $val [" url "]." </td><td> ". $val [" from "]." </td><td> ". $val [" Key "]." </td><td> ". $val [" title "]."
	</td></tr> ";
Var_dump ($val); echo "</table>";

Ii. insertion of data
$collection = $db->selectcollection (' grppic ');//mongodb do not have to set up a collection (table), there is no table structure
$doc = Array (
	' title ' =>  ' title ',
	' thumb ' => ' thumb ',
	//can automatically set each record
	' URL ' => (object) Array ("1.jpg", "2.jpg", "3.jpg" according to the size of the data) , "4.jpg")
);
$cord = Array (' title ' => ' title ', ' Thumb ' => ' thumb ', ' url ' => ' aaaaa ', ' title ' => ' inserted ');
$collection->insert ($doc);
echo $doc [' _id '];
Returns the last inserted $id value
//Update action
//The following number ID is the value of the returned $id
iii. Modification of data
$id =new mongoid ("5271c4f0da466aec0c000000");//each inserted recording system gives a $id, which can be known by find
$where =array (' _id ' => ( object) $id);
$newdata =array (' URLs ' => (object) Array ("A.jpg", "b.jpg"));
$result = $collection->update ($where, Array (' $set ' => $newdata));
MongoDB also supports batch updates, similar to relational databases, where you can update all documents for a given condition, and if you want to do so, you need to set the multiple value of the options to true.
$result = $collection->update ($where, Array (' $set ' => $newdata), array (' multiple ' =>true));

$cursor = $collection->find ();
while ($cursor->hasnext ()) {
    var_dump ($cursor->getnext ());
}

iv. deletion of data

$collection->remove ();//Empty datasheet
//$collection->remove (Array (' _id ' => new Mongoid ($id));//Delete a record
//$collection->remove (Array (' genre ' => ' drama '), Array (' Justone ' => True));//Add Justone parameter, Only one record that meets the criteria is deleted, others do not delete//$collection->drop ();//drop efficiency is higher than remove
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.