1
- # Bulk CRUD
1
+ # Bulk CRUD Operations
2
2
3
3
``` php
4
4
<?php
5
5
6
- $hannes = array(
7
- "name" => "Hannes",
8
- "nick" => "bjori",
9
- "citizen" => "Iceland",
10
- );
11
- $hayley = array(
12
- "name" => "Hayley",
13
- "nick" => "Alien Ninja",
14
- "citizen" => "USA",
15
- );
16
- $jonpall = array(
17
- "name" => "Jon Pall",
18
- "nick" => "unknown",
19
- "citizen" => "Iceland",
20
- );
21
-
22
- /* Ordered bulk is executed in the same order as we add the operations to it.
23
- * If operation fails the execution stops and no further operations executed.
24
- * For unordered bulk, the operations can be executed in any order by the database
25
- * in an attempt to optimize its workload. An operation failure will not stop
26
- * the exection of the rest of the operations.
27
- * Default: true
28
- */
29
- $ordered = true;
30
-
31
- $bulk = new MongoDB\Driver\BulkWrite($ordered);
6
+ $hannes = [
7
+ "name" => "Hannes",
8
+ "nick" => "bjori",
9
+ "citizen" => "Iceland",
10
+ ];
11
+ $hayley = [
12
+ "name" => "Hayley",
13
+ "nick" => "Alien Ninja",
14
+ "citizen" => "USA",
15
+ ];
16
+ $jonpall = [
17
+ "name" => "Jon Pall",
18
+ "nick" => "unknown",
19
+ "citizen" => "Iceland",
20
+ ];
21
+
22
+ /* Ordered bulk operations (default) are executed in the same order that we add
23
+ * the write operations. If an operation fails, execution stops and no further
24
+ * operations are attempted. For unordered bulk operations, the operations may
25
+ * be executed in any order by the database in an attempt to optimize its
26
+ * workload. An operation failure within an unordered batch will not stop
27
+ * execution of additional write operations in that batch. */
28
+ $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
32
29
33
30
?>
34
31
```
35
32
36
- ## CREATE
33
+ ## Create
37
34
38
35
``` php
39
36
<?php
40
37
41
- /* Argument#1 The document (array/ object) to insert.
42
- * Returns the generated BSON\ObjectID if no _id was provided
43
- */
38
+ /* Specify the document to insert (array or object) as the first argument. If a
39
+ * document does not already have an " _id" field, a generated
40
+ * MongoDB\BSON\ObjectId will be returned. * /
44
41
$hannes_id = $bulk->insert($hannes);
45
42
$hayley_id = $bulk->insert($hayley);
46
43
$jonpall_id = $bulk->insert($jonpall);
47
44
48
45
?>
49
46
```
50
47
51
- ## UPDATE
48
+ ## Update
52
49
53
50
``` php
54
51
<?php
55
52
56
- /* Arguments
57
- * #1: array, search criteria
58
- * #2: array, the object or operations to apply to the matching document
59
- * #3: array, wire protocol options:
60
- * limit: integer, 0 or 1,
61
- * 0: Apply to all matching documents
62
- * 1: Only apply to the first matching document
63
- * upsert: boolean
64
- * true: Insert new document matching the criteria
65
- * false: Do not insert new document if no matches are found
66
- */
53
+ /* Arguments:
54
+ *
55
+ * #1 (array|object): search criteria to select the document(s) for updating
56
+ * #2 (array|object): replacement document or atomic operations to apply
57
+ * #3 (array): update options
58
+ * * limit (integer): Updates all matching documents when 0 (false).
59
+ * Otherwise, only the first matching document is updated.
60
+ * * upsert (boolean): If there is no matching document, create a new
61
+ * document from $filter and $update. */
67
62
$bulk->update(
68
- array( "_id" => $hayley_id) ,
69
- array( '$set' => array( "citizen" => "Iceland")) ,
70
- array( "limit" => 1, "upsert" => false)
63
+ [ "_id" => $hayley_id] ,
64
+ [ '$set' => [ "citizen" => "Iceland"]] ,
65
+ [ "limit" => 1, "upsert" => false]
71
66
);
72
67
$bulk->update(
73
- array( "citizen" => "Iceland") ,
74
- array( '$set' => array( "viking" => true)) ,
75
- array( "limit" => 0, "upsert" => false)
68
+ [ "citizen" => "Iceland"] ,
69
+ [ '$set' => [ "viking" => true]] ,
70
+ [ "limit" => 0, "upsert" => false]
76
71
);
77
72
$bulk->update(
78
- array( "name" => "Chuck Norris") ,
79
- array( '$set' => array( "viking" => false)) ,
80
- array( "limit" => 1, "upsert" => true)
81
- ) ;
73
+ [ "name" => "Chuck Norris"] ,
74
+ [ '$set' => [ "viking" => false]] ,
75
+ [ "limit" => 1, "upsert" => true]
76
+ ] ;
82
77
83
78
?>
84
79
```
85
80
86
- ## DELETE
81
+ ## Delete
87
82
88
83
``` php
89
84
<?php
90
85
91
- /* Arguments
92
- * #1: array, search criteria
93
- * #2: array, wire protocol options:
94
- * limit: integer, 0 or 1,
95
- * 0: Delete all matching documents
96
- * 1: Only delete the first matching document
97
- */
98
- $bulk->delete(array("_id" => $jonpall_id), array("limit" => 1));
86
+ /* Arguments:
87
+ *
88
+ * #1 (array|object): search criteria to select the document(s) for deleting
89
+ * #3 (array): delete options
90
+ * * limit (integer): Deletes all matching documents when 0 (false).
91
+ * Otherwise, only the first matching document is deleted. */
92
+ $bulk->delete(
93
+ ["_id" => $jonpall_id],
94
+ ["limit" => 1]
95
+ );
99
96
100
97
?>
101
98
```
@@ -104,28 +101,36 @@ $bulk->delete(array("_id" => $jonpall_id), array("limit" => 1));
104
101
105
102
``` php
106
103
<?php
104
+
105
+ // Construct a write concern
106
+ $wc = new MongoDB\Driver\WriteConcern(
107
+ // Guarantee that writes are acknowledged by a majority of our nodes
108
+ MongoDB\Driver\WriteConcern::MAJORITY,
109
+ // But only wait 1000ms because we have an application to run!
110
+ 1000
111
+ );
112
+
107
113
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
108
- $wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY);
109
114
$result = $manager->executeBulkWrite("db.collection", $bulk, $wc);
110
115
111
116
printf("insertedCount: %d\n", $result->getInsertedCount());
112
- printf("matchedCount: %d\n", $result->getMatchedCount());
117
+ printf("matchedCount: %d\n", $result->getMatchedCount());
113
118
printf("modifiedCount: %d\n", $result->getModifiedCount());
114
119
printf("upsertedCount: %d\n", $result->getUpsertedCount());
115
- printf("deletedCount: %d\n", $result->getDeletedCount());
120
+ printf("deletedCount: %d\n", $result->getDeletedCount());
116
121
117
122
foreach ($result->getUpsertedIds() as $index => $id) {
118
- printf("upsertedId: '%s', index: %d\n", $id, $index);
123
+ printf("upsertedId: '%s', index: %d\n", $id, $index);
119
124
}
120
125
121
- $query = new MongoDB\Driver\Query(array( "viking" => false) );
126
+ $query = new MongoDB\Driver\Query([ "viking" => false] );
122
127
$cursor = $manager->executeQuery("db.collection", $query);
123
- /* Note that var_dump()ing the $cursor will print out all sorts of debug information
124
- * about the cursor, such as ReadPreferences used, the query executed, namespace,
125
- * query flags, and the current bulk information */
126
- var_dump(iterator_to_array($cursor));
128
+
129
+ /* Note that var_dump()-ing the $cursor directly will print out additional debug
130
+ * information about the cursor, such as the read preferenced used, the query
131
+ * executed, namespace, query flags, and information about the current batch of
132
+ * results from the server. Instead, we'll simply var_dump() the results. */
133
+ var_dump($cursor->toArray());
127
134
128
135
?>
129
136
```
130
-
131
-
0 commit comments