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.insert.html
67 lines (64 loc) · 5.17 KB
/
mongo.tutorial.insert.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>Inserting a Document</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.collection.html">« Getting A Collection</a></li>
<li style="float: right;"><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne »</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>Inserting a Document</li>
</ul>
</div>
<div id="layout">
<div id="layout-content"><div id="mongo.tutorial.insert" class="section">
<h2 class="title">Inserting a Document</h2>
<p class="para">
Associative arrays are the basic object that can be saved to a collection in
the database. A somewhat random "document" might be:
</p>
<div class="example" id="mongo.tutorial.insert-data-example">
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$doc </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"MongoDB"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"type" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"database"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"count" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"info" </span><span style="color: #007700">=> (object)array( </span><span style="color: #DD0000">"x" </span><span style="color: #007700">=> </span><span style="color: #0000BB">203</span><span style="color: #007700">, </span><span style="color: #DD0000">"y" </span><span style="color: #007700">=> </span><span style="color: #0000BB">102</span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">"versions" </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">"0.9.7"</span><span style="color: #007700">, </span><span style="color: #DD0000">"0.9.8"</span><span style="color: #007700">, </span><span style="color: #DD0000">"0.9.9"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<p class="para">
Note that you can have nested arrays and objects. The driver will always
store an associative array as an object in the database. A
numerically indexed array is stored as an array in case the keys start at
0 and are not interrupted, and as an object if the array keys don't start
at 0 or have gaps (ie: <code class="literal">0, 1, 4, 5</code>).
</p>
<p class="para">
To insert this document, use <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>:
</p>
<div class="example" id="mongo.tutorial.insert-example-2">
<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">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">( </span><span style="color: #0000BB">$doc </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<div class="section" id="mongo.tutorial.insert.seealso">
<h2 class="title">See Also</h2>
<p class="para">
The API documentation on <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>
contains more information about inserting data.
</p>
</div>
</div></div></div></body></html>