Skip to content

Commit 8d08c4a

Browse files
committed
Update code examples for 0.2
1 parent 34bdc8a commit 8d08c4a

File tree

4 files changed

+112
-93
lines changed

4 files changed

+112
-93
lines changed

examples/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
4+
// Dependencies were installed with Composer and this is the main project
5+
$loader = require_once __DIR__ . '/../vendor/autoload.php';
6+
} elseif (file_exists(__DIR__ . '/../../../../autoload.php')) {
7+
// We're installed as a dependency in another project's `vendor` directory
8+
$loader = require_once __DIR__ . '/../../../../autoload.php';
9+
} else {
10+
throw new Exception('Can\'t find autoload.php. Did you install dependencies with Composer?');
11+
}

examples/Collection-bulkWrite.php renamed to examples/bulkwrite.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
<?php
2-
require __DIR__ . "/" . "../vendor/autoload.php";
32

4-
function dumpWriteResults(MongoDB\WriteResult $result) {
5-
printf("Inserted %d documents, upserted %d, updated %d and deleted %d\n",
6-
$result->getInsertedCount(), $result->getUpsertedCount(),
7-
$result->getModifiedCount(), $result->getDeletedCount()
3+
require_once __DIR__ . "/bootstrap.php";
4+
5+
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
6+
$collection = new MongoDB\Collection($manager, "phplib_demo.bulkwrite");
7+
8+
function dumpWriteResults(MongoDB\BulkWriteResult $result)
9+
{
10+
printf("Inserted %d documents, upserted %d, updated %d, and deleted %d\n",
11+
$result->getInsertedCount(),
12+
$result->getUpsertedCount(),
13+
$result->getModifiedCount(),
14+
$result->getDeletedCount()
815
);
916

1017
if ($result->getUpsertedCount()) {
1118
foreach ($result->getUpsertedIds() as $index => $id) {
12-
printf("upsertedId[%d]: %s", $index, $id);
19+
printf("upsertedId[%d]: %s\n", $index, $id);
1320
}
1421
}
1522
}
16-
function dumpCollection($collection) {
17-
printf("\n---\nDumping all documents in: %s.%s\n",
23+
24+
function dumpCollection($collection)
25+
{
26+
printf("Dumping all documents in: %s.%s\n",
1827
$collection->getDatabaseName(),
1928
$collection->getCollectionName()
2029
);
@@ -26,9 +35,6 @@ function dumpCollection($collection) {
2635
printf("Found %d documents\n", $n);
2736
}
2837

29-
30-
$manager = new MongoDB\Manager("mongodb://localhost:27017");
31-
$collection = new MongoDB\Collection($manager, "crud.bulkWrite");
3238
$result = $collection->bulkWrite([
3339
[
3440
"insertOne" => [
@@ -61,8 +67,9 @@ function dumpCollection($collection) {
6167
]);
6268

6369
dumpWriteResults($result);
70+
echo "\n";
6471
dumpCollection($collection);
65-
72+
echo "\n";
6673

6774
$result = $collection->bulkWrite([
6875
[
@@ -84,9 +91,10 @@ function dumpCollection($collection) {
8491
],
8592
]);
8693

87-
echo "\n\n";
8894
dumpWriteResults($result);
95+
echo "\n";
8996
dumpCollection($collection);
97+
echo "\n";
9098

9199
$result = $collection->bulkWrite([
92100
[
@@ -96,7 +104,6 @@ function dumpCollection($collection) {
96104
],
97105
]);
98106

99-
echo "\n\n";
100107
dumpWriteResults($result);
108+
echo "\n";
101109
dumpCollection($collection);
102-

examples/import-json.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/write.php

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,96 @@
11
<?php
22

3-
require __DIR__ . "/../src/QueryFlags.php";
4-
require __DIR__ . "/../src/CursorType.php";
5-
require __DIR__ . "/../src/InsertResult.php";
6-
require __DIR__ . "/../src/DeleteResult.php";
7-
require __DIR__ . "/../src/UpdateResult.php";
8-
require __DIR__ . "/../src/Collection.php";
9-
10-
11-
$manager = new MongoDB\Manager("mongodb://localhost:27017");
12-
13-
14-
$collection = new MongoDB\Collection($manager, "crud.examples");
15-
$hannes = array(
16-
"name" => "Hannes",
17-
"nick" => "bjori",
18-
"citizen" => "Iceland",
19-
);
20-
$hayley = array(
21-
"name" => "Hayley",
22-
"nick" => "Ninja",
23-
"citizen" => "USA",
24-
);
25-
$bobby = array(
3+
require_once __DIR__ . "/bootstrap.php";
4+
5+
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
6+
$collection = new MongoDB\Collection($manager, "phplib_demo.write");
7+
8+
$hannes = [
9+
"name" => "Hannes",
10+
"nick" => "bjori",
11+
"citizen" => "Iceland",
12+
];
13+
$hayley = [
14+
"name" => "Hayley",
15+
"nick" => "Ninja",
16+
"citizen" => "USA",
17+
];
18+
$bobby = [
2619
"name" => "Robert Fischer",
2720
"nick" => "Bobby Fischer",
2821
"citizen" => "USA",
29-
);
30-
$kasparov = array(
22+
];
23+
$kasparov = [
3124
"name" => "Garry Kimovich Kasparov",
3225
"nick" => "Kasparov",
3326
"citizen" => "Russia",
34-
);
35-
$spassky = array(
27+
];
28+
$spassky = [
3629
"name" => "Boris Vasilievich Spassky",
3730
"nick" => "Spassky",
3831
"citizen" => "France",
39-
);
32+
];
33+
4034

4135
try {
4236
$result = $collection->insertOne($hannes);
43-
printf("Inserted _id: %s\n", $result->getInsertedId());
37+
printf("Inserted _id: %s\n\n", $result->getInsertedId());
38+
4439
$result = $collection->insertOne($hayley);
45-
printf("Inserted _id: %s\n", $result->getInsertedId());
40+
printf("Inserted _id: %s\n\n", $result->getInsertedId());
41+
4642
$result = $collection->insertOne($bobby);
47-
printf("Inserted _id: %s\n", $result->getInsertedId());
43+
printf("Inserted _id: %s\n\n", $result->getInsertedId());
4844

49-
$count = $collection->count(array("nick" => "bjori"));
50-
printf("Searching for nick => bjori, should have only one result: %d\n", $count);
45+
$count = $collection->count(["nick" => "bjori"]);
46+
printf("Searching for nick => bjori, should have only one result: %d\n\n", $count);
5147

5248
$result = $collection->updateOne(
53-
array("citizen" => "USA"),
54-
array('$set' => array("citizen" => "Iceland"))
49+
["citizen" => "USA"],
50+
['$set' => ["citizen" => "Iceland"]]
5551
);
56-
printf("Updated: %s (out of expected 1)\n", $result->getModifiedCount());
52+
printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount());
5753

58-
$result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query"));
54+
$cursor = $collection->find(
55+
["citizen" => "Iceland"],
56+
["comment" => "Excellent query"]
57+
);
5958
echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
60-
foreach($result as $document) {
59+
foreach($cursor as $document) {
6160
var_dump($document);
6261
}
62+
echo "\n";
6363
} catch(Exception $e) {
6464
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
6565
exit;
6666
}
6767

68+
6869
try {
69-
$result = $collection->find();
70+
$cursor = $collection->find();
7071
echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
71-
foreach($result as $document) {
72+
foreach($cursor as $document) {
7273
var_dump($document);
7374
}
75+
echo "\n";
76+
7477
$result = $collection->distinct("citizen");
7578
echo "Distinct countries:\n";
7679
var_dump($result);
80+
echo "\n";
7781

7882
echo "aggregate\n";
79-
$aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => true, "batchSize" => 2));
83+
$result = $collection->aggregate(
84+
[
85+
['$project' => ["name" => 1, "_id" => 0]],
86+
],
87+
[ "useCursor" => true, "batchSize" => 2 ]
88+
);
8089
printf("Should be 3 different people\n");
81-
foreach($aggregate as $person) {
90+
foreach($result as $person) {
8291
var_dump($person);
8392
}
84-
93+
echo "\n";
8594
} catch(Exception $e) {
8695
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
8796
exit;
@@ -90,32 +99,32 @@
9099

91100
try {
92101
$result = $collection->updateMany(
93-
array("citizen" => "Iceland"),
94-
array('$set' => array("viking" => true))
102+
["citizen" => "Iceland"],
103+
['$set' => ["viking" => true]]
95104
);
96-
97105
printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount());
98106
$result = $collection->find();
99107
foreach($result as $document) {
100108
var_dump($document);
101109
}
110+
echo "\n";
102111
} catch(Exception $e) {
103112
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
104113
exit;
105114
}
106115

107116

108117
try {
109-
echo "This is the trouble maker\n";
110118
$result = $collection->replaceOne(
111-
array("nick" => "Bobby Fischer"),
112-
array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway")
119+
["nick" => "Bobby Fischer"],
120+
["name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway"]
113121
);
114122
printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount());
115123
$result = $collection->find();
116124
foreach($result as $document) {
117125
var_dump($document);
118126
}
127+
echo "\n";
119128
} catch(Exception $e) {
120129
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
121130
exit;
@@ -124,34 +133,46 @@
124133

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

129-
$result = $collection->deleteMany(array("citizen" => "Iceland"));
130-
printf("Deleted: %d (out of expected 2)\n", $result->getDeletedCount());
138+
$result = $collection->deleteMany(["citizen" => "Iceland"]);
139+
printf("Deleted: %d (out of expected 2)\n\n", $result->getDeletedCount());
131140
} catch(Exception $e) {
132141
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
133142
exit;
134143
}
135144

145+
136146
try {
137147
echo "FindOneAndReplace\n";
138-
$result = $collection->findOneAndReplace($spassky, $kasparov, array("upsert" => true));
148+
$result = $collection->findOneAndReplace(
149+
$spassky,
150+
$kasparov,
151+
["upsert" => true]
152+
);
139153
echo "Kasparov\n";
140154
var_dump($result);
155+
echo "\n";
141156

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

146165
echo "Deleting him, he isn't Croatian just yet\n";
147-
$result = $collection->findOneAndDelete(array("citizen" => "Croatia"));
166+
$result = $collection->findOneAndDelete(["citizen" => "Croatia"]);
148167
var_dump($result);
168+
echo "\n";
149169

150170
echo "This should be empty\n";
151-
$result = $collection->find(array());
171+
$result = $collection->find();
152172
foreach($result as $document) {
153173
var_dump($document);
154174
}
175+
echo "\n";
155176
} catch(Exception $e) {
156177
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
157178
exit;
@@ -162,7 +183,7 @@
162183
$result = $collection->bulkWrite(
163184
// Required writes param (an array of operations)
164185
[
165-
// Like explain(), operations identified by single key
186+
// Operations identified by single key
166187
[
167188
'insertOne' => [
168189
['x' => 1]
@@ -204,11 +225,10 @@
204225
printf("deletedCount: %d\n", $result->getDeletedCount());
205226

206227
foreach ($result->getUpsertedIds() as $index => $id) {
207-
printf("upsertedId[%d]: %s", $index, $id);
228+
printf("upsertedId[%d]: %s\n", $index, $id);
208229
}
209230

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

0 commit comments

Comments
 (0)