Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 9376

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Adding Multiple Documents</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.counting.html">Counting Documents in A Collection</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.multiple" class="section">
   <h2 class="title">Adding Multiple Documents</h2>
   <p class="para">
    In order to do more interesting things with queries, let&#039;s add multiple
    simple documents to the collection. These documents will just be of the form
    <em>array( &quot;i&quot; =&gt; <span class="replaceable">value</span> );</em> and we
    can do this fairly efficiently in a loop:
   </p>
   <div class="example" id="mongo.tutorial.insert.multiple-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br />for&nbsp;(&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++&nbsp;)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(&nbsp;array(&nbsp;</span><span style="color: #DD0000">'i'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"field</span><span style="color: #007700">{</span><span style="color: #0000BB">$i</span><span style="color: #007700">}</span><span style="color: #DD0000">"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">)&nbsp;);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    Notice that we can insert arrays with different keys into the same
    collection. This aspect is what we mean when we say that MongoDB is
    &quot;schema-free&quot;. In the example above each document has an
    <em>i</em> field, but also a field name in the form of
    <em>field</em> + <em>$i</em>.
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.findone.html">Finding Documents using MongoCollection::findOne</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.counting.html">Counting Documents in A Collection</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>