-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41973 Read Landing Page #148
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
a5f2990
f11a302
fc6dfda
1680c76
e236d7a
ac0e57d
23738fd
36b360c
63c1926
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,67 @@ | ||||||||||||||||||||||||||||||||||||||||||||
<?php | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/vendor/autoload.php'; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
use MongoDB\Client; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$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->sample_mflix->movies; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Find One | ||||||||||||||||||||||||||||||||||||||||||||
// start-find-one | ||||||||||||||||||||||||||||||||||||||||||||
$document = $collection->findOne(['year' => 1994]); | ||||||||||||||||||||||||||||||||||||||||||||
echo json_encode($document) , "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
// end-find-one | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Find Multiple | ||||||||||||||||||||||||||||||||||||||||||||
// start-find-multiple | ||||||||||||||||||||||||||||||||||||||||||||
$resultsMultiple = $collection->find(['year' => 1970]); | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($resultsMultiple as $doc) { | ||||||||||||||||||||||||||||||||||||||||||||
echo json_encode($doc) , "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
// end-find-multiple | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Count Document | ||||||||||||||||||||||||||||||||||||||||||||
// start-count | ||||||||||||||||||||||||||||||||||||||||||||
$result = $collection->countDocuments([]); | ||||||||||||||||||||||||||||||||||||||||||||
echo "Number of documents: " . $result; | ||||||||||||||||||||||||||||||||||||||||||||
// end-count | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Count Specific Documents | ||||||||||||||||||||||||||||||||||||||||||||
// start-count-specific | ||||||||||||||||||||||||||||||||||||||||||||
$result = $collection->countDocuments(['year' => 2010]); | ||||||||||||||||||||||||||||||||||||||||||||
echo "Number of companies founded in 2010: " . $result; | ||||||||||||||||||||||||||||||||||||||||||||
// end-count-specific | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Estimated Count | ||||||||||||||||||||||||||||||||||||||||||||
// start-count-estimate | ||||||||||||||||||||||||||||||||||||||||||||
$result = $collection->estimatedDocumentCount(); | ||||||||||||||||||||||||||||||||||||||||||||
echo "Estimated number of documents: " . $result; | ||||||||||||||||||||||||||||||||||||||||||||
// end-count-estimate | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Distinct Values | ||||||||||||||||||||||||||||||||||||||||||||
// start-distinct | ||||||||||||||||||||||||||||||||||||||||||||
$results = $collection->distinct('year', []); | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($results as $value) { | ||||||||||||||||||||||||||||||||||||||||||||
echo json_encode($value) . PHP_EOL; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
// end-distinct | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Data Changes | ||||||||||||||||||||||||||||||||||||||||||||
// start-change-stream | ||||||||||||||||||||||||||||||||||||||||||||
$changeStream = $collection->watch(); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
for ($changeStream->rewind(); true; $changeStream->next()) { | ||||||||||||||||||||||||||||||||||||||||||||
if ( ! $changeStream->valid()) { | ||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
$event = $changeStream->current(); | ||||||||||||||||||||||||||||||||||||||||||||
echo toJSON($event) . PHP_EOL; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if ($event['operationType'] === 'invalidate') { | ||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+56
to
+66
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.
Suggested change
Have you looked at our changestream example? 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. Because of time and for consistency with examples on the already approved Monitor Data Changes page, I'll keep as is here. I can look into revising the examples as you've suggested here in a future piece of work. Thanks! |
||||||||||||||||||||||||||||||||||||||||||||
// end-change-stream | ||||||||||||||||||||||||||||||||||||||||||||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.