How to implement PHP query data from MongoDb

Source: Internet
Author: User
Tags mongodb query

I. Software environment (version not required) PHP v5.6 extension: MongoDB nginx v1.11 MongoDB v3.2

Note: MongoDB extensions must be installed

Ii. Connection $client = new Mongoclient ($server, $option);

$server variable is a string that describes the server to which you want to connect

Mongodb://[username:[email protected]]host1[:p ort1][,host2[:p ort2:],...] /db

It is necessary to:

Username database user name password database password Host server address port database listening ports three, select Table (collection) Choose a collection of databases $db = $client->{$db _name};

Returns the MongoDB class document portal

$collection = $db->{$collection _name};

Returns the Mongocollection class document portal

Iv. query Data $cursor = $collection->find ($filter);

Returns the Mongocursor class document portal

1. $filter Parameters

Mongoshell Find function Parameters use: Portal

PHP Code writing

A document with a query value greater than value $filter [$gtKey] = Array (' $gt ' = = $value); Regexkey fuzzy Query $filter [$regexKey] = new Mongoregex ('/'. $regexValue. ' /‘); $collection->find ($filter);

Attention:

Fuzzy queries need to be used with the Mongoregex class, otherwise when the comparison size is not recognized, the $value value needs to be consistent with the type stored in MongoDB 2. Using Mongocursor to perform operations such as paging//return mongocursor, you can use the Mongocursor method to traverse the data//or use Iterator_to_array ($cursor) to convert the array $cursor = $cursor ->sort (Array ($field = 1 or 1 '))//1 is the positive order, 1 is the reverse order->skip ($start)//Data starting position->limit ($size); Data End position is $start+ $size//return int type value $count = $cursor->find ($filter)->count (); $data = Iterator_to_array ($cursor)

No more explaining how to traverse data using Mongocursor

V. Summary

Using PHP to connect MongoDB query data is generally very simple. Connection, according to the criteria of query, paging, three steps.

Precautions:

Using singleton mode to get the MongoDB instance fuzzy query needs to be done with the Mongoregex class operation to close the connection * *

That ' s right. This article is first published in March, Buttimes, new themes is launched for sale, so we've stripped out t Hemes which is no longer available andadded eight new examples to keep things up to date.

1. Introduce yourself

Emotional design is hugely important on the web; Add the "human element" to your portfolio and your visitors would more likely connect with the work you show them. Make sure they know who they ' re dealing with, who's responsible for the photos, and who it's they ' ll need to get in touch With if they want to know more.

The responsive photography Theme by Themegoods offers a homepage layout which does this really well. Your name, Your signature, a touch of personality up front and centre.

How to implement PHP query data from MongoDb

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.