예제 #1
0
    if ($result->getUpsertedCount()) {
        foreach ($result->getUpsertedIds() as $index => $id) {
            printf("upsertedId[%d]: %s\n", $index, $id);
        }
    }
}
function dumpCollection($collection)
{
    printf("Dumping all documents in: %s.%s\n", $collection->getDatabaseName(), $collection->getCollectionName());
    $n = 0;
    foreach ($collection->find() as $document) {
        var_dump($document);
        $n++;
    }
    printf("Found %d documents\n", $n);
}
$result = $collection->bulkWrite([["insertOne" => [["name" => "Hannes Magnusson", "company" => "10gen"]]], ["insertOne" => [["name" => "Jeremy Mikola", "company" => "10gen"]]], ["updateMany" => [["company" => "10gen"], ['$set' => ["company" => "MongoDB"]]]], ["updateOne" => [["name" => "Hannes Magnusson"], ['$set' => ["viking" => true]]]]]);
dumpWriteResults($result);
echo "\n";
dumpCollection($collection);
echo "\n";
$result = $collection->bulkWrite([["deleteOne" => [["company" => "MongoDB"]]], ["updateOne" => [["name" => "Hannes Magnusson"], ['$set' => ["nationality" => "Icelandic"]], ["upsert" => true]]], ["deleteMany" => [["nationality" => ['$ne' => "Icelandic"]]]]]);
dumpWriteResults($result);
echo "\n";
dumpCollection($collection);
echo "\n";
$result = $collection->bulkWrite([["deleteMany" => [[]]]]);
dumpWriteResults($result);
echo "\n";
dumpCollection($collection);
예제 #2
0
$ids[] = createDocument($compCollection, "Battery 400Wh", ['watt-hours' => 400]);
$ids[] = createDocument($compCollection, "Motor", ['voltage' => 36, 'wattage' => 250, 'manufacturer' => getManufacturerId($manuCollection, 'yamaha'), 'list_price' => ['currency' => 'GBP', 'value' => 300]]);
$ids[] = createDocument($compCollection, "Haibike SDURO frame", ['material' => 'Aluminium', 'size_inches' => 27.5, 'description' => "6061, All MNT, 4-Link System, Yamaha-Interface, hydroforced tubes, 150mm", 'list_price' => ['currency' => 'GBP', 'value' => 400]]);
// Special group for the drivetrain
$dtIds = [];
$dtIds[] = createDocument($compCollection, 'Haibike sDuro crank', ['material' => 'Aluminium', 'gears' => 2, 'list_price' => ['currency' => 'GBP', 'value' => 45]]);
$dtIds[] = createDocument($compCollection, 'Front Derailleur', ['manufacturer' => getManufacturerId($manuCollection, 'shimano'), 'list_price' => ['currency' => 'GBP', 'value' => 40]]);
$dtIds[] = createDocument($compCollection, "Rear Derailleur", ['manufacturer' => getManufacturerId($manuCollection, 'shimano'), 'line' => 'Deore XT', 'model' => 'M 786 Shadow Plus', 'gears' => 10, 'list_price' => ['currency' => 'GBP', 'value' => 50]]);
$dtIds[] = createDocument($compCollection, "Cassette", ['description' => 'Sram PG 1020 11-36 Teeth', 'list_price' => ['currency' => 'GBP', 'value' => 60]]);
// Finally put the drivetrain together
$ids[] = createDocument($compCollection, "Haibike SDURO Drivetrain", ['speeds' => 20, 'components' => createIdsGroup($dtIds)]);
// Let's create a full bike
createDocument($compCollection, "Haibike SDURO AllMtn RC", ['full_build' => true, 'components' => createIdsGroup($ids)]);
// Show the full builds, which include all of the above components
echo "Bikes:\n";
dumpCollection($compCollection, ['full_build' => true]);
// Use an aggregation pipeline to sum the prices of the components
$pipeline = [['$match' => ['list_price' => ['$exists' => true]]], ['$project' => ['price' => '$list_price.value']], ['$group' => ['_id' => null, 'total' => ['$sum' => '$price']]]];
$result = $compCollection->aggregate($pipeline);
if (isset($result['result'][0]['total'])) {
    $price = $result['result'][0]['total'];
    echo "Components total price: GBP{$price}\n";
}
/**
 * Interesting extensions:
 * 
 * > Group by the currency - should be possible with aggregation
 * > Deriving a component sum just for one build would be trickier - expect that would
 *		need map-reduce functions in JavaScript, since the components are referenced
 *		recursively.
 */