This repository was archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathmongo.tutorial.multi.query.html
67 lines (63 loc) · 4.51 KB
/
mongo.tutorial.multi.query.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Getting A Set of Documents With a Query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />
</head>
<body class="docs"><div class="navbar navbar-fixed-top">
<div class="navbar-inner clearfix">
<ul class="nav" style="width: 100%">
<li style="float: left;"><a href="mongo.tutorial.criteria.html">« Setting Criteria for a Query</a></li>
<li style="float: right;"><a href="mongo.tutorial.indexes.html">Creating An Index »</a></li>
</ul>
</div>
</div>
<div id="breadcrumbs" class="clearfix">
<ul class="breadcrumbs-container">
<li><a href="index.html">PHP Manual</a></li>
<li><a href="mongo.tutorial.html">Tutorial</a></li>
<li>Getting A Set of Documents With a Query</li>
</ul>
</div>
<div id="layout">
<div id="layout-content"><div id="mongo.tutorial.multi.query" class="section">
<h2 class="title">Getting A Set of Documents With a Query</h2>
<p class="para">
We can use the query to get a set of documents from our collection. For
example, if we wanted to get all documents where <code class="literal">"i"</code>
> <code class="literal">50</code>, we could write:
</p>
<div class="example" id="mongo.tutorial.multi.query-example">
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$connection </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$connection</span><span style="color: #007700">-></span><span style="color: #0000BB">database</span><span style="color: #007700">-></span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"i" </span><span style="color: #007700">=> array( </span><span style="color: #DD0000">'$gt' </span><span style="color: #007700">=> </span><span style="color: #0000BB">50 </span><span style="color: #007700">) ); </span><span style="color: #FF8000">//note the single quotes around '$gt'<br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">( </span><span style="color: #0000BB">$query </span><span style="color: #007700">);<br /><br />while ( </span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">hasNext</span><span style="color: #007700">() )<br />{<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">getNext</span><span style="color: #007700">() );<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>
which should print the documents where <code class="literal">"i"</code> >
<code class="literal">50</code>. We could also get a range, say
<code class="literal">20 < i <= 30</code>:
</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
<?php
$connection = new MongoClient();
$collection = $connection->database->collectionName;
$query = array( 'i' => array( '$gt' => 20, "\$lte" => 30 ) );
$cursor = $collection->find( $query );
while ( $cursor->hasNext() )
{
var_dump( $cursor->getNext() );
}
?>
</pre></div>
</div>
</div>
<p class="para">
Remember to always escape the $-symbol or use single quotes. Otherwise PHP
will interpret it to be the variable <var class="varname">$gt</var>.
</p>
</div></div></div></body></html>