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

3 4 5 6 7 8 9 10 11 12 13
require_once __DIR__ . "/bootstrap.php";

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "phplib_demo.write");

$hannes = [
    "name"    => "Hannes", 
    "nick"    => "bjori",
    "citizen" => "Iceland",
];
$hayley = [
14
    "name"    => "Bayley",
15 16 17 18
    "nick"    => "Ninja",
    "citizen" => "USA",
];
$bobby = [
Hannes Magnusson's avatar
Hannes Magnusson committed
19 20 21
    "name" => "Robert Fischer",
    "nick" => "Bobby Fischer",
    "citizen" => "USA",
22 23
];
$kasparov = [
24 25 26
    "name"    => "Garry Kimovich Kasparov",
    "nick"    => "Kasparov",
    "citizen" => "Russia",
27 28
];
$spassky = [
29 30 31
    "name"    => "Boris Vasilievich Spassky",
    "nick"    => "Spassky",
    "citizen" => "France",
32 33
];

34 35

try {
Hannes Magnusson's avatar
Hannes Magnusson committed
36
    $result = $collection->insertOne($hannes);
37 38
    printf("Inserted _id: %s\n\n", $result->getInsertedId());

Hannes Magnusson's avatar
Hannes Magnusson committed
39
    $result = $collection->insertOne($hayley);
40 41
    printf("Inserted _id: %s\n\n", $result->getInsertedId());

Hannes Magnusson's avatar
Hannes Magnusson committed
42
    $result = $collection->insertOne($bobby);
43
    printf("Inserted _id: %s\n\n", $result->getInsertedId());
Hannes Magnusson's avatar
Hannes Magnusson committed
44

45 46
    $count = $collection->count(["nick" => "bjori"]);
    printf("Searching for nick => bjori, should have only one result: %d\n\n", $count);
Hannes Magnusson's avatar
Hannes Magnusson committed
47 48

    $result = $collection->updateOne(
49 50
        ["citizen" => "USA"],
        ['$set' => ["citizen" => "Iceland"]]
Hannes Magnusson's avatar
Hannes Magnusson committed
51
    );
52
    printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount());
Hannes Magnusson's avatar
Hannes Magnusson committed
53

54 55 56 57
    $cursor = $collection->find(
        ["citizen" => "Iceland"],
        ["comment" => "Excellent query"]
    );
58
    echo "Searching for citizen => Iceland, verify Bayley is now Icelandic\n";
59
    foreach($cursor as $document) {
Hannes Magnusson's avatar
Hannes Magnusson committed
60 61
        var_dump($document);
    }
62
    echo "\n";
63
} catch(Exception $e) {
64
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
65 66 67
    exit;
}

68

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

77 78 79
    $result = $collection->distinct("citizen");
    echo "Distinct countries:\n";
    var_dump($result);
80
    echo "\n";
Hannes Magnusson's avatar
Hannes Magnusson committed
81

82
    echo "aggregate\n";
83 84 85 86 87 88
    $result = $collection->aggregate(
        [
            ['$project' => ["name" => 1, "_id" => 0]],
        ],
        [ "useCursor" => true, "batchSize" => 2 ]
    );
89
    printf("Should be 3 different people\n");
90
    foreach($result as $person) {
91 92
        var_dump($person);
    }
93
    echo "\n";
94 95 96 97 98 99 100
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}


try {
Hannes Magnusson's avatar
Hannes Magnusson committed
101
    $result = $collection->updateMany(
102 103
        ["citizen" => "Iceland"],
        ['$set' => ["viking" => true]]
Hannes Magnusson's avatar
Hannes Magnusson committed
104
    );
105
    printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount());
Hannes Magnusson's avatar
Hannes Magnusson committed
106 107 108 109
    $result = $collection->find();
    foreach($result as $document) {
        var_dump($document);
    }
110
    echo "\n";
111 112 113 114 115 116 117
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}


try {
118
    $result = $collection->replaceOne(
119 120
        ["nick" => "Bobby Fischer"],
        ["name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway"]
121
    );
122
    printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount());
123 124 125 126
    $result = $collection->find();
    foreach($result as $document) {
        var_dump($document);
    }
127
    echo "\n";
128 129 130 131
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}
Hannes Magnusson's avatar
Hannes Magnusson committed
132

133 134

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

138 139
    $result = $collection->deleteMany(["citizen" => "Iceland"]);
    printf("Deleted: %d (out of expected 2)\n\n", $result->getDeletedCount());
140 141 142 143 144
} catch(Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}

145

146 147
try {
    echo "FindOneAndReplace\n";
148 149 150 151 152
    $result = $collection->findOneAndReplace(
        $spassky,
        $kasparov,
        ["upsert" => true]
    );
153 154
    echo "Kasparov\n";
    var_dump($result);
155
    echo "\n";
156

157
    echo "Returning the old document where he was Russian\n";
158 159 160 161
    $result = $collection->findOneAndUpdate(
        $kasparov,
        ['$set' => ["citizen" => "Croatia"]]
    );
162
    var_dump($result);
163
    echo "\n";
164

165
    echo "Deleting him, he isn't Croatian just yet\n";
166
    $result = $collection->findOneAndDelete(["citizen" => "Croatia"]);
167
    var_dump($result);
168
    echo "\n";
169 170

    echo "This should be empty\n";
171
    $result = $collection->find();
172 173 174
    foreach($result as $document) {
        var_dump($document);
    }
175
    echo "\n";
176
} catch(Exception $e) {
177
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
178
    exit;
Hannes Magnusson's avatar
Hannes Magnusson committed
179
}
180

181 182 183 184 185

try {
    $result = $collection->bulkWrite(
        // Required writes param (an array of operations)
        [
186
            // Operations identified by single key
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 215 216 217 218 219 220 221 222 223 224 225 226 227
            [
                '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) {
228
        printf("upsertedId[%d]: %s\n", $index, $id);
229 230 231 232 233 234
    }

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