Skip to content

Commit a328109

Browse files
committed
Merge remote-tracking branch 'upstream/php-standardization' into DOCSP-41971-bulk
2 parents 996e74b + df79843 commit a328109

23 files changed

+2229
-0
lines changed

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ensures that we always use the latest version of the script
2+
if [ -f build-site.sh ]; then
3+
rm build-site.sh
4+
fi
5+
6+
7+
curl https://raw.githubusercontent.com/mongodb/docs-worker-pool/netlify-poc/scripts/build-site.sh -o build-site.sh
8+
sh build-site.sh

netlify.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[[integrations]]
2+
name = "snooty-cache-plugin"
3+
4+
# Production context: all deploys from the Production branch
5+
# set in your site’s Branches settings in the UI will inherit
6+
# these settings.
7+
[build]
8+
publish = "snooty/public"
9+
command = ". ./build.sh"

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ toc_landing_pages = [
1818
"/reference/class/MongoDBModelDatabaseInfo",
1919
"/reference/class/MongoDBModelIndexInfo",
2020
"/get-started",
21+
"/write",
2122
]
2223

2324
[substitutions]
9.01 KB
Loading

source/includes/indexes/indexes.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
7+
8+
$database = $client->sample_mflix;
9+
$collection = $database->movies;
10+
11+
// start-list-indexes
12+
foreach ($collection->listIndexes() as $indexInfo) {
13+
echo $indexInfo;
14+
}
15+
// end-list-indexes
16+
17+
// start-remove-index
18+
$collection->dropIndex('_title_');
19+
// end-remove-index
20+
21+
// start-remove-all-indexes
22+
$collection->dropIndexes();
23+
// end-remove-all-indexes
24+
25+
// start-index-single
26+
$indexName = $collection->createIndex(['title' => 1]);
27+
// end-index-single
28+
29+
// start-index-single-query
30+
$document = $collection->findOne(['title' => 'Sweethearts']);
31+
echo json_encode($document), PHP_EOL;
32+
// end-index-single-query
33+
34+
// start-multikey
35+
$indexName = $collection->createIndex(['cast' => 1]);
36+
// end-multikey
37+
38+
// start-index-array-query
39+
$document = $collection->findOne(
40+
['cast' => ['$in' => ['Aamir Khan', 'Kajol']]]
41+
);
42+
echo json_encode($document), PHP_EOL;
43+
// end-index-array-query
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
7+
8+
$database = $client->sample_db;
9+
$collection = $database->sample_coll;
10+
11+
// start-to-json
12+
function toJSON(object $document): string
13+
{
14+
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
15+
}
16+
// end-to-json
17+
18+
// start-single-field
19+
$indexName = $collection->createIndex(['<field name>' => 1]);
20+
// end-single-field
21+
22+
// start-compound
23+
$indexName = $collection->createIndex(
24+
['<field name 1>' => 1, '<field name 2>' => 1]
25+
);
26+
// end-compound
27+
28+
// start-multikey
29+
$indexName = $collection->createIndex(['<array field name>' => 1]);
30+
// end-multikey
31+
32+
// start-search-create
33+
$indexName = $collection->createSearchIndex(
34+
['mappings' => ['dynamic' => true]],
35+
['name' => '<Search index name>']
36+
);
37+
// end-search-create
38+
39+
// start-search-list
40+
foreach ($collection->listSearchIndexes() as $indexInfo) {
41+
echo toJSON($indexInfo), PHP_EOL;
42+
}
43+
// end-search-list
44+
45+
// start-search-update
46+
$collection->updateSearchIndex(
47+
'<Search index name>',
48+
['mappings' => [
49+
'dynamic' => false,
50+
'fields' => [
51+
'<string field name>' => [
52+
'type' => 'string',
53+
'analyzer' => 'lucene.standard'
54+
]
55+
]
56+
]]
57+
);
58+
// end-search-update
59+
60+
// start-search-delete
61+
$collection->dropSearchIndex('<Search index name>');
62+
// end-search-delete
63+
64+
// start-text
65+
$indexName = $collection->createIndex(['<field name>' => 'text']);
66+
// end-text
67+
68+
// start-geo
69+
$indexName = $collection->createIndex(
70+
[ '<GeoJSON object field>' => '2dsphere']
71+
);
72+
// end-geo
73+
74+
// start-unique
75+
$indexName = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
76+
// end-unique
77+
78+
// start-wildcard
79+
$indexName = $collection->createIndex(['$**' => 1]);
80+
// end-wildcard
81+
82+
// start-clustered
83+
$options = [
84+
'clusteredIndex' => [
85+
'key' => ['_id' => 1],
86+
'unique' => true
87+
]
88+
];
89+
90+
$database->createCollection('<collection name>', $options);
91+
// end-clustered
92+
93+
// start-list
94+
foreach ($collection->listIndexes() as $indexInfo) {
95+
echo $indexInfo;
96+
}
97+
// end-list
98+
99+
// start-remove
100+
$collection->dropIndex('<index name>');
101+
// end-remove
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Sample Application
2+
~~~~~~~~~~~~~~~~~~
3+
4+
You can use the following sample application to test the code examples on this
5+
page. To use the sample application, perform the following steps:
6+
7+
1. Ensure you have the {+php-library+} installed in your project.
8+
#. Copy the following code and paste it into a new ``.php`` file.
9+
#. Copy a code example from this page and paste it on the specified
10+
lines in the file.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
7+
8+
$collection = $client->selectCollection('<database>', '<collection>');
9+
10+
// Start example code here
11+
12+
// End example code here
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
5+
$client = new MongoDB\Client($uri);
6+
$collection = $client->db->coll;
7+
8+
// Inserts one document that stores the specified values
9+
// start-insert-one
10+
$result = $collection->insertOne([
11+
'<field name 1>' => '<value 1>',
12+
'<field name 2>' => '<value 2>',
13+
]);
14+
// end-insert-one
15+
16+
// Inserts multiple documents that store the specified values
17+
// start-insert-multiple
18+
$result = $collection->insertMany(
19+
[
20+
'<field name 1>' => '<value 1>',
21+
'<field name 2>' => '<value 2>',
22+
],
23+
[
24+
'<field name 1>' => '<value 1>',
25+
'<field name 2>' => '<value 2>',
26+
],
27+
);
28+
// end-insert-multiple
29+
30+
// Updates a document that matches the specified criteria
31+
// start-update-one
32+
$result = $collection->updateOne(
33+
['<field to match>' => '<value to match>'],
34+
['$set' => ['<field name>' => '<value>']],
35+
);
36+
// end-update-one
37+
38+
// Updates all documents that match the specified criteria
39+
// start-update-multiple
40+
$result = $collection->updateMany(
41+
['<field to match>' => '<value to match>'],
42+
['$set' => ['<field name>' => '<value>']],
43+
);
44+
// end-update-multiple
45+
46+
// start-replace-one
47+
$result = $collection->replaceOne(
48+
['<field to match>' => '<value to match>'],
49+
[
50+
'<new field 1>' => '<value 1>',
51+
'<new field 2>' => '<value 2>',
52+
],
53+
);
54+
// end-replace-one
55+
56+
// Deletes a document that matches the specified criteria
57+
// start-delete-one
58+
$result = $collection->deleteOne(['<field name>' => '<value>']);
59+
// end-delete-one
60+
61+
// Deletes all documents that match the specified criteria
62+
// start-delete-multiple
63+
$result = $collection->deleteMany(['<field name>' => '<value>']);
64+
// end-delete-multiple
65+
66+
// Runs a bulk operation based on the instructions in each array entry
67+
// start-bulk-write
68+
$result = $collection->bulkWrite(
69+
[
70+
[
71+
'insertOne' => [
72+
['<field name>' => '<value>'],
73+
],
74+
],
75+
[
76+
'replaceOne' => [
77+
['<field to match>' => '<value to match>'],
78+
[
79+
'<first new field>' => '<value>',
80+
'<second new field>' => '<value>',
81+
],
82+
],
83+
],
84+
[
85+
'updateOne' => [
86+
['<field to match>' => '<value to match>'],
87+
['$set' => ['<field to update>' => '<value to update>']],
88+
],
89+
],
90+
[
91+
'updateMany' => [
92+
['<field to match>' => '<value to match>'],
93+
['$set' => ['<field to update>' => '<value to update>']],
94+
],
95+
],
96+
[
97+
'deleteOne' => [
98+
['<field name>' => '<value>'],
99+
],
100+
],
101+
[
102+
'deleteMany' => [
103+
['<field name>' => '<value>'],
104+
],
105+
],
106+
]
107+
);
108+
// end-bulk-write
109+
110+
// Stores a file in a GridFS bucket and writes data to the file
111+
// start-gridfs-upload
112+
$bucket = $client->selectDatabase('<database name>')->selectGridFSBucket();
113+
$stream = $bucket->openUploadStream('<file name>');
114+
fwrite($stream, '<data>');
115+
fclose($stream);
116+
// end-gridfs-upload

source/includes/write/delete.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
5+
$client = new MongoDB\Client($uri);
6+
7+
// start-db-coll
8+
$collection = $client->sample_restaurants->restaurants;
9+
// end-db-coll
10+
11+
// Deletes a document that has a "name" value of "Ready Penny Inn"
12+
// start-delete-one
13+
$collection->deleteOne(['name' => 'Ready Penny Inn']);
14+
// end-delete-one
15+
16+
// Deletes documents that have a "borough" value of "Brooklyn"
17+
// start-delete-many
18+
$collection->deleteMany(['borough' => 'Brooklyn']);
19+
// end-delete-many
20+
21+
// Deletes matching documents and attaches a comment to the operation
22+
// start-delete-options
23+
$collection->deleteMany(
24+
['name' => new MongoDB\BSON\Regex('Mongo')],
25+
['comment' => 'Deleting Mongo restaurants'],
26+
);
27+
// end-delete-options
28+
29+
// Deletes and prints the number of documents that have a "cuisine" value of "Greek"
30+
// start-delete-count
31+
$result = $collection->deleteMany(['cuisine' => 'Greek']);
32+
echo 'Deleted documents: ' , $result->getDeletedCount() , PHP_EOL;
33+
// end-delete-count
34+

0 commit comments

Comments
 (0)