PHP 5.4.33 Released

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

There are no user contributed notes for this page.
To Top