write.php 6.45 KB
Newer Older
1 2
<?php

3 4 5 6 7 8
require __DIR__ . "/../src/QueryFlags.php";
require __DIR__ . "/../src/CursorType.php";
require __DIR__ . "/../src/InsertResult.php";
require __DIR__ . "/../src/DeleteResult.php";
require __DIR__ . "/../src/UpdateResult.php";
require __DIR__ . "/../src/Collection.php";
9 10 11 12 13 14 15 16 17 18 19 20 21


$manager = new MongoDB\Manager("mongodb://localhost:27017");


$collection = new MongoDB\Collection($manager, "crud.examples");
$hannes = array(
	"name"    => "Hannes", 
	"nick"    => "bjori",
	"citizen" => "Iceland",
);
$hayley = array(
	"name"    => "Hayley",
Hannes Magnusson's avatar
Hannes Magnusson committed
22
	"nick"    => "Ninja",
23 24
	"citizen" => "USA",
);
Hannes Magnusson's avatar
Hannes Magnusson committed
25 26 27 28
$bobby = array(
    "name" => "Robert Fischer",
    "nick" => "Bobby Fischer",
    "citizen" => "USA",
29
);
30 31 32 33 34 35 36 37 38 39
$kasparov = array(
    "name"    => "Garry Kimovich Kasparov",
    "nick"    => "Kasparov",
    "citizen" => "Russia",
);
$spassky = array(
    "name"    => "Boris Vasilievich Spassky",
    "nick"    => "Spassky",
    "citizen" => "France",
);
40 41

try {
Hannes Magnusson's avatar
Hannes Magnusson committed
42
    $result = $collection->insertOne($hannes);
43
    printf("Inserted _id: %s\n", $result->getInsertedId());
Hannes Magnusson's avatar
Hannes Magnusson committed
44
    $result = $collection->insertOne($hayley);
45
    printf("Inserted _id: %s\n", $result->getInsertedId());
Hannes Magnusson's avatar
Hannes Magnusson committed
46
    $result = $collection->insertOne($bobby);
47
    printf("Inserted _id: %s\n", $result->getInsertedId());
Hannes Magnusson's avatar
Hannes Magnusson committed
48

49 50
    $count = $collection->count(array("nick" => "bjori"));
    printf("Searching for nick => bjori, should have only one result: %d\n", $count);
Hannes Magnusson's avatar
Hannes Magnusson committed
51 52 53 54 55

    $result = $collection->updateOne(
        array("citizen" => "USA"),
        array('$set' => array("citizen" => "Iceland"))
    );
56
    printf("Updated: %s (out of expected 1)\n", $result->getModifiedCount());
Hannes Magnusson's avatar
Hannes Magnusson committed
57 58 59 60 61 62

    $result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query"));
    echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
    foreach($result as $document) {
        var_dump($document);
    }
63
} catch(Exception $e) {
64
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
65 66 67 68
    exit;
}

try {
Hannes Magnusson's avatar
Hannes Magnusson committed
69
    $result = $collection->find();
70
    echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
Hannes Magnusson's avatar
Hannes Magnusson committed
71 72 73
    foreach($result as $document) {
        var_dump($document);
    }
74 75 76
    $result = $collection->distinct("citizen");
    echo "Distinct countries:\n";
    var_dump($result);
Hannes Magnusson's avatar
Hannes Magnusson committed
77

78 79
    echo "aggregate\n";
    $aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => true, "batchSize" => 2));
80
    printf("Should be 3 different people\n");
81 82 83
    foreach($aggregate as $person) {
        var_dump($person);
    }
84

85 86 87 88 89 90 91
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}


try {
Hannes Magnusson's avatar
Hannes Magnusson committed
92
    $result = $collection->updateMany(
93 94
        array("citizen" => "Iceland"),
        array('$set' => array("viking" => true))
Hannes Magnusson's avatar
Hannes Magnusson committed
95 96
    );

97
    printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount());
Hannes Magnusson's avatar
Hannes Magnusson committed
98 99 100 101
    $result = $collection->find();
    foreach($result as $document) {
        var_dump($document);
    }
102 103 104 105 106 107 108 109
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}


try {
    echo "This is the trouble maker\n";
110 111 112 113
    $result = $collection->replaceOne(
        array("nick" => "Bobby Fischer"),
        array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway")
    );
114
    printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount());
115 116 117 118
    $result = $collection->find();
    foreach($result as $document) {
        var_dump($document);
    }
119 120 121 122
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}
Hannes Magnusson's avatar
Hannes Magnusson committed
123

124 125

try {
126
    $result = $collection->deleteOne($document);
127
    printf("Deleted: %d (out of expected 1)\n", $result->getDeletedCount());
128

Hannes Magnusson's avatar
Hannes Magnusson committed
129
    $result = $collection->deleteMany(array("citizen" => "Iceland"));
130
    printf("Deleted: %d (out of expected 2)\n", $result->getDeletedCount());
131 132 133 134 135 136 137 138 139 140 141
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}

try {
    echo "FindOneAndReplace\n";
    $result = $collection->findOneAndReplace($spassky, $kasparov, array("upsert" => true));
    echo "Kasparov\n";
    var_dump($result);

142 143 144 145
    echo "Returning the old document where he was Russian\n";
    $result = $collection->findOneAndUpdate($kasparov, array('$set' => array("citizen" => "Croatia")));
    var_dump($result);

146 147 148 149 150 151 152 153 154
    echo "Deleting him, he isn't Croatian just yet\n";
    $result = $collection->findOneAndDelete(array("citizen" => "Croatia"));
    var_dump($result);

    echo "This should be empty\n";
    $result = $collection->find(array());
    foreach($result as $document) {
        var_dump($document);
    }
155
} catch(Exception $e) {
156
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
157
    exit;
Hannes Magnusson's avatar
Hannes Magnusson committed
158
}
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

try {
    $result = $collection->bulkWrite(
        // Required writes param (an array of operations)
        [
            // Like explain(), operations identified by single key
            [
                'insertOne' => [
                    ['x' => 1]
                ],
            ],
            [
                'updateMany' => [
                    ['x' => 1],
                    ['$set' => ['x' => 2]],
                ],
            ],
            [
                'updateOne' => [
                    ['x' => 3],
                    ['$set' => ['x' => 4]],
                    // Optional params are still permitted
                    ['upsert' => true],
                ],
            ],
            [
                'deleteOne' => [
                    ['x' => 1],
                ],
            ],
            [
                'deleteMany' => [
                    // Required arguments must still be specified
                    [],
                ],
            ],
        ],
        // Optional named params in an associative array
        ['ordered' => false]
    );
    printf("insertedCount: %d\n", $result->getInsertedCount());
    printf("matchedCount: %d\n", $result->getMatchedCount());
    printf("modifiedCount: %d\n", $result->getModifiedCount());
    printf("upsertedCount: %d\n", $result->getUpsertedCount());
    printf("deletedCount: %d\n", $result->getDeletedCount());

    foreach ($result->getUpsertedIds() as $index => $id) {
        printf("upsertedId[%d]: %s", $index, $id);
    }

} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    echo $e->getTraceAsString(), "\n";
    exit;
}