PHP Simple Query MongoDB

Source: Internet
Author: User
Tags mongodb php tutorial mongodb support

Used to play MySQL, from this week began to contact MongoDB, and use PHP to check a wave of MongoDB. In fact, and MySQL almost, just changed a database, so the query statement writing is not the same. Here's a quick summary.

First, MongoDB

MySQL is a relational database, MongoDB is a relational database, NoSQL (not only SQL I always thought was no-sql).



MongoDB features (this part is the video course of the course I made notes, I think the talk is still more clear.) ) 1. No data structure constraints (1) There is no concept of table structure, each record can have a completely different structure (2) business development Convenient and quick (3) SQL database to define the table structure in advance and then use 2. Full index Support (1) Redis can only query Key-value (2) hbase single index, two-level index needs to build itself and MongoDB support one-touch index, multiple key index, array index, FULL-TEXT Index, geographical index, etc. 3. Convenient redundancy and extension (1) Replication sets guarantee data security (2) fragmentation Extended Data scale 4. Good support for complete documentation and full drive

Second, PHP operation MongoDB

The understanding of MongoDB is limited to this. The next task is to use PHP to operate MongoDB, where the main concern is simple query.

This time the study a little quick success, think hurriedly learn to use, there is no mongodb have a very systematic, very in-depth understanding. (Of course, time is not allowed, or obsessive-compulsive disorder I guess I will learn from scratch.) )

PHP Operation MongoDB, I learned from another tutorial, now put the address here.

MongoDB PHP Tutorial

I thought this would be a good way to complete the task, in fact, I still pattern.

It's not interesting to say, I'll just go to the code.

<?php//Connect $m = new Mongoclient ();

Select a Database $db = $m->ht_email;

Select a collection (analogous to a relational database ' s table) $collection = $db->email; $CC = isset ($_request[' cc '])?
($_request[' CC ']): ""; $from = Isset ($_request[' from '])?
($_request[' from ']): ""; $to = Isset ($_request[' to '])?
($_request[' to ']): ""; $attach = isset ($_request[' attach '))?
($_request[' Attach ']): ""; $subject = Isset ($_request[' subject '))?

($_request[' subject ']): "";
where $query = Array (); if (!empty ($CC)) {$query = Array_merge (Array (' Cc_user ' =>new mongoregex ("/.*". $cc. ").
*/i ")), $query); } if (!empty ($from)) {$query = Array_merge (Array (' From_user ' =>new mongoregex ("/.*". $from. ").
*/i ")), $query); } if (!empty ($to)) {$query = Array_merge (Array (' To_user ' =>new mongoregex ("/.*". $to. ").
*/i ")), $query); } if (!empty ($attach)) {$query = Array_merge (Array (' Attachments ' =>new mongoregex ("/.*". $attach. ").
*/i ")), $query); } if (!empty ($subject)) {$query = Array_merge (arrAy (' Subject ' =>new Mongoregex ("/.*". $subject. ")
*/i ")), $query); }//page $pagesize = 10;  The number of data bars displayed per page $page = isset ($_get[' page ')]? Intval ($_get[' page '): 1;  Get page Information $offset = ($page-1) * $pagesize; Offset (///Gets the value of the first parameter of limit offset, if the first page is (1-1) *10=0, the second page is (2-1) *10=10.
  (Incoming pages-1) * Data per page Gets the value of limit first parameter) $total = $collection->find ($query)->skip (0)->count (true);  $pagenum = Ceil ($total/$pagesize);
  Gets the total number of pages if ($page <1) {$page = 1;
  } if ($page > $pagenum) {$page = $pagenum;
    } if ($page <10) {$start = 1;
  $end = 9;
    } elseif ($page >=10 && $page < ($pagenum -9+1)) {$start = $page-4;
  $end = $page +4;
    } elseif ($page >= ($pagenum -9+1)) {$start = $pagenum -9+1;
  $end = $pagenum;
  } $pre = $page-1;

  $next = $page +1;

$page _params = "&from= $from &to= $to &subject= $subject &cc= $cc &attach= $attach";

Find $cursor = $collection->find ($query)->limit ($pagesize)->skip ($offset); ?>   
The code has a comment, and I'll explain briefly the course of my study.

The first is to connect the database Ah, here because the web and MongoDB on a server, so directly omitted the parameters.

Select the database and collection.

The next step is to receive the parameters from the front page. Needless to say, the name of input corresponds to the PHP request.

Then write the query condition, which is where in SQL.

First determine if you have received the parameters, if not null, start to write the condition.

The MongoDB condition is written in an array, which field you want to look up and what the value is.

The Mongoregex here are used to blur the match.

Array_merge is the merging of arrays to implement multiple conditional queries.

The next page is pagination, which was mentioned before. Attention is paid to the calculation of $total. You need to use the syntax count () for MongoDB statistics.

Also note that to write the paging parameter.

The query statement is find. It would be nice to take the query condition we just constructed as a parameter.

The page-flipping query is implemented using limit and skip. This analogy to MySQL's limit $pagesize, $offset easy to understand.

The code to page pages shows the same as MySQL, where it is not written.


At this point, you can easily query the MongoDB data.

MongoDB is a new way, hoping to learn more in this area.



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.