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.html
82 lines (56 loc) · 6.7 KB
/
mongo.tutorial.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!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>Tutorial</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.manual.html">« Manual</a></li>
<li style="float: right;"><a href="mongo.tutorial.connecting.html">Making a Connection »</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.manual.html">Manual</a></li>
<li>Tutorial</li>
</ul>
</div>
<div id="layout">
<div id="layout-content"><div id="mongo.tutorial" class="chapter">
<h1>Tutorial</h1>
<h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="mongo.tutorial.connecting.html">Making a Connection</a></li><li><a href="mongo.tutorial.selectdb.html">Getting a Database</a></li><li><a href="mongo.tutorial.collection.html">Getting A Collection</a></li><li><a href="mongo.tutorial.insert.html">Inserting a Document</a></li><li><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne</a></li><li><a href="mongo.tutorial.insert.multiple.html">Adding Multiple Documents</a></li><li><a href="mongo.tutorial.counting.html">Counting Documents in A Collection</a></li><li><a href="mongo.tutorial.cursor.html">Using a Cursor to Get All of the Documents</a></li><li><a href="mongo.tutorial.criteria.html">Setting Criteria for a Query</a></li><li><a href="mongo.tutorial.multi.query.html">Getting A Set of Documents With a Query</a></li><li><a href="mongo.tutorial.indexes.html">Creating An Index</a></li></ul>
<div class="warning"><strong class="warning">Warning</strong>
<p class="para">This extension is deprecated. Instead,
the <a href="" class="link">MongoDB</a> extension should be
used.</p>
</div>
<p class="para">
This is the official MongoDB driver for PHP.
</p>
<p class="para">Here's a quick code sample that connects, inserts documents, queries for
documents, iterates through query results, and disconnects from MongoDB.
There are more details on each step in the tutorial below.
</p>
<div class="example" id="mongo.tutorial.basics">
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// connect<br /></span><span style="color: #0000BB">$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// select a database<br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">comedy</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// select a collection (analogous to a relational database's table)<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">cartoons</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// add a record<br /></span><span style="color: #0000BB">$document </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"title" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Calvin and Hobbes"</span><span style="color: #007700">, </span><span style="color: #DD0000">"author" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Bill Watterson" </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// add another record, with a different "shape"<br /></span><span style="color: #0000BB">$document </span><span style="color: #007700">= array( </span><span style="color: #DD0000">"title" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"XKCD"</span><span style="color: #007700">, </span><span style="color: #DD0000">"online" </span><span style="color: #007700">=> </span><span style="color: #0000BB">true </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// find everything in the collection<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">();<br /><br /></span><span style="color: #FF8000">// iterate through the results<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$cursor </span><span style="color: #007700">as </span><span style="color: #0000BB">$document</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$document</span><span style="color: #007700">[</span><span style="color: #DD0000">"title"</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Calvin and Hobbes
XKCD
</pre></div>
</div>
</div>
</div>
</div></div></body></html>