downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Counting Documents in A Collection> <Finding Documents using MongoCollection::findOne
[edit] Last updated: Fri, 14 Jun 2013

view this page in

Adding Multiple Documents

In order to do more interesting things with queries, let's add multiple simple documents to the collection. These documents will just be of the form array( "i" => value ); and we can do this fairly efficiently in a loop:

<?php
$connection 
= new MongoClient();
$collection $connection->database->collectionName;

for ( 
$i 0$i 100$i++ )
{
    
$collection->insert( array( 'i' => $i"field{$i}=> $i ) );
}
?>

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 "schema-free". In the example above each document has an i field, but also a field name in the form of field + $i.



add a note add a note User Contributed Notes Adding Multiple Documents - [0 notes]
There are no user contributed notes for this page.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites