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 * 2 ) );
}
?>
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.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Manual
- Making a Connection
- Getting a Database
- Getting A Collection
- Inserting a Document
- Finding Documents using MongoCollection::findOne
- Adding Multiple Documents
- Counting Documents in A Collection
- Using a Cursor to Get All of the Documents
- Setting Criteria for a Query
- Getting A Set of Documents With a Query
- Creating An Index
Коментарии
404 Not Found