<!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> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.collection.html">Getting A Collection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne</a></div> <div class="up"><a href="mongo.tutorial.html">Tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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: <em>0, 1, 4, 5</em>). </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><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.collection.html">Getting A Collection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne</a></div> <div class="up"><a href="mongo.tutorial.html">Tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>