Skip to content

DOCSP-41983: indexes landing pg #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions source/includes/usage-examples/index-code-examples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
$client = new MongoDB\Client($uri);

$database = $client->sample_db;
$collection = $database->sample_coll;

// start-to-json
function toJSON(object $document): string
{
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
}
// end-to-json

// start-single-field
$indexName = $collection->createIndex(['<field name>' => 1]);
// end-single-field

// start-compound
$indexName = $collection->createIndex(
['<field name 1>' => 1, '<field name 2>' => 1]
);
// end-compound

// start-multikey
$indexName = $collection->createIndex(['<array field name>' => 1]);
// end-multikey

// start-search-create
$indexName = $collection->createSearchIndex(
['mappings' => ['dynamic' => true]],
['name' => '<Search index name>']
);
// end-search-create

// start-search-list
foreach ($collection->listSearchIndexes() as $indexInfo) {
echo toJSON($indexInfo) . PHP_EOL;
}
// end-search-list

// start-search-update
$collection->updateSearchIndex(
'<Search index name>',
['mappings' => [
'dynamic' => false,
'fields' => [
'<string field name>' => [
'type' => 'string',
'analyzer' => 'lucene.standard'
]
]
]]
);
// end-search-update

// start-search-delete
$collection->dropSearchIndex('<Search index name>');
// end-search-delete

// start-text
$indexName = $collection->createIndex(['<field name>' => 'text']);
// end-text

// start-geo
$indexName = $collection->createIndex(
[ '<GeoJSON object field>' => '2dsphere']
);
// end-geo

// start-unique
$indexName = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
// end-unique

// start-wildcard
$indexName = $collection->createIndex(['$**' => 1]);
// end-wildcard

// start-clustered
$options = [
'clusteredIndex' => [
'key' => ['_id' => 1],
'unique' => true
]
];

$database->createCollection('<collection name>', $options);
// end-clustered

// start-list
foreach ($collection->listIndexes() as $indexInfo) {
echo $indexInfo;
}
// end-list

// start-remove
$collection->dropIndex('<index name>');
// end-remove
10 changes: 10 additions & 0 deletions source/includes/usage-examples/sample-app-intro.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Sample Application
~~~~~~~~~~~~~~~~~~

You can use the following sample application to test the code examples on this
page. To use the sample application, perform the following steps:

1. Ensure you have the {+php-library+} installed in your project.
#. Copy the following code and paste it into a new ``.php`` file.
#. Copy a code example from this page and paste it on the specified
lines in the file.
12 changes: 12 additions & 0 deletions source/includes/usage-examples/sample-app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
$client = new MongoDB\Client($uri);

$collection = $client->selectCollection('<database>', '<collection>');

// Start example code here

// End example code here
1 change: 1 addition & 0 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MongoDB PHP Library
/read
/write
/aggregation
/indexes
/tutorial
/upgrade
/reference
Expand Down
Loading
Loading