-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41965: Read and write settings #146
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
Changes from 7 commits
3d7ae37
857753b
2aab86a
d0e6934
0966a2a
e31a674
79e3608
a36917d
1bcd46e
2f570f5
e48c5b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use MongoDB\Driver\ReadConcern; | ||
use MongoDB\Driver\ReadPreference; | ||
use MongoDB\Driver\WriteConcern; | ||
use MongoDB\Client; | ||
|
||
// start-client-settings | ||
$clientOptions = [ | ||
'readPreference' => 'secondary', | ||
'readConcernLevel' => 'local', | ||
'w' => '2', | ||
]; | ||
|
||
$client = new Client('mongodb://localhost:27017', $clientOptions); | ||
// end-client-settings | ||
|
||
// start-client-settings-uri | ||
$uri = 'mongodb://localhost:27017/?readPreference=secondary&readConcernLevel=local&w=2'; | ||
$client = new Client($uri); | ||
// end-client-settings-uri | ||
|
||
// start-session-settings | ||
$sessionOptions = [ | ||
'readPreference' => new ReadPreference(ReadPreference::PRIMARY_PREFERRED), | ||
'readConcern' => new ReadConcern(ReadConcern::LOCAL), | ||
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), | ||
]; | ||
|
||
$session = $client->startSession($sessionOptions); | ||
// end-session-settings | ||
|
||
// start-transaction-settings | ||
$transactionOptions = [ | ||
'readPreference' => new ReadPreference(ReadPreference::PRIMARY), | ||
'readConcern' => new ReadConcern(ReadConcern::MAJORITY), | ||
'writeConcern' => new WriteConcern(1), | ||
]; | ||
|
||
// Start the transaction | ||
$session->startTransaction($transactionOptions); | ||
// end-transaction-settings | ||
|
||
// Sets read and write settings for the "test_database" database | ||
// start-database-settings | ||
$readPreference = new ReadPreference(ReadPreference::PRIMARY_PREFERRED); | ||
$readConcern = new ReadConcern(ReadConcern::AVAILABLE); | ||
$writeConcern = new WriteConcern(WriteConcern::MAJORITY); | ||
|
||
$db = $client->selectDatabase('test_database', [ | ||
'readPreference' => $readPreference, | ||
'readConcern' => $readConcern, | ||
'writeConcern' => $writeConcern, | ||
]); | ||
// end-database-settings | ||
|
||
// Sets read and write settings for the "test_collection" collection | ||
// start-collection-settings | ||
$readPreference = new ReadPreference(ReadPreference::SECONDARY_PREFERRED); | ||
$readConcern = new ReadConcern(ReadConcern::AVAILABLE); | ||
$writeConcern = new WriteConcern(0); | ||
|
||
$collection = $client->selectCollection('test_database', 'test_collection', [ | ||
'readPreference' => $readPreference, | ||
'readConcern' => $readConcern, | ||
'writeConcern' => $writeConcern, | ||
]); | ||
|
||
// end-collection-settings | ||
|
||
// Instructs the library to prefer reads from secondary replica set members | ||
// located in New York, followed by a secondary in San Francisco, and | ||
// lastly fall back to any secondary. | ||
// start-tag-set | ||
$readPreference = new ReadPreference( | ||
ReadPreference::RP_SECONDARY, | ||
[ | ||
['dc' => 'ny'], | ||
['dc' => 'sf'], | ||
[], | ||
], | ||
); | ||
|
||
$db = $client->selectDatabase( | ||
'test_database', | ||
['readPreference' => $readPreference], | ||
); | ||
|
||
// end-tag-set | ||
|
||
// Instructs the library to distribute reads between members within 35 milliseconds | ||
// of the closest member's ping time | ||
// start-local-threshold | ||
$options = [ | ||
'replicaSet' => 'repl0', | ||
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), | ||
'localThresholdMS' => 35, | ||
]; | ||
|
||
$client = new Client('<connection string>', [], $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: standardize the way connection strings are presented on the page (an earlier example uses the default local port) |
||
// end-local-threshold |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ MongoDB PHP Library | |
/aggregation | ||
/indexes | ||
/security | ||
/read-write-pref | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: I think that this section might be better suited to be under the read/write drawers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure whether to put it under read or write - I guess read if I had to choose, since there's more content on read settings configuration? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think you need to put it in a drawer but just in the list under read and write |
||
/tutorial | ||
/upgrade | ||
/reference | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.