MongoD...eStream(3pm)
π MongoDB::ChangeStream(3pm) β like, the manual, but make it make sense
okay bestie, so this is the User Contributed Perl Documentation page for MongoDB::ChangeStream(3pm), and I'm about to explain it to you the way it deserves to be explained. no cap. π¦
β¨ NAME β¨
MongoDB::ChangeStream β a stream that serves up update info for collections, like a lil news feed but for your database. π°
πΈ VERSION πΈ
version v2.2.2
π SYNOPSIS π
$stream = $collection->watch( $pipeline, $options );
while(1) {
# This inner loop will only iterate until there are no more
# changes available.
while (my $change = $stream->next) {
...
}
}
notice the nesting, bestie β the outer while(1) keeps you alive, the inner one drains whatever's there and then dips. π
π¦ DESCRIPTION π¦
This class is the whole vibe of change stream results β literally exactly what comes back from the watch method over in MongoDB::Collection. That's the tea. β
π STREAM METHODS π
π next
$change_stream = $collection->watch(...);
$change = $change_stream->next;
It waits for the next change to happen in the collection, and then hands it to you. iconic behavior, honestly. π«Ά
Note: this method will wait for however many milliseconds you passed asmaxAwaitTimeMStowatchinMongoDB::Collectionβ or the server's default wait-time if you didn't. It will not wait indefinitely. so like, don't build your whole life around it hanging around forever. π
π§ get_resume_token
So here's the situation: you can peek at the _id on each ChangeDocument and use that as a resume token. BUT β and this is the glow-up β since MongoDB 4.2, aggregate and getMore responses also include a postBatchResumeToken. Drivers pick one or the other when they're auto-resuming. two tokens, one job, messy. π
This method grabs the same resume token that would be used to automatically resume. So if you're the kind of person who wants to store that token somewhere β and you are, you organized queen β use this method, because it gives you the most up to date one. periodt. β
For instance:
if ($local_change) {
process_change($local_change);
}
eval {
my $change_stream = $coll->watch([], { resumeAfter => $local_resume_token });
while ( my $change = $change_stream->next) {
$local_resume_token = $change_stream->get_resume_token;
$local_change = $change;
process_change($local_change);
}
};
if (my $err = $@) {
$log->error($err);
}
In this case the current change is always persisted locally, resume token included β so on restart your app can still process that change while making sure the change stream picks up from the right logical time in the oplog. chef's kiss. π₯
Important though: it is the application's responsibility to make sure process_change is idempotent. This design merely makes a reasonable effort to process each change at least once. translation: it tries its best, it does not pinky-promise. π
π SEE ALSO π
The Change Streams manual section β https://docs.mongodb.com/manual/changeStreams/.
The Change Streams specification β https://github.com/mongodb/specifications/blob/master/source/change-streams.rst.
iykyk, go read them. π
π AUTHORS π
- David Golden
<david@mongodb.com> - Rassi
<rassi@mongodb.com> - Mike Friedman
<friedo@friedo.com> - Kristina Chodorow
<k.chodorow@gmail.com> - Florian Ragwitz
<rafl@debian.org>
look at this roster. legends only. π
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2020 by MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
free software, slay. π
and that's the whole page, bestie β no crumbs left. this was MongoD...eStream(3pm) from perl v5.40.1, dated 2025-04-03. go forth and stream your changes. πβ¨
π¦ where this page comes from
- debian trixie: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- ubuntu resolute: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- ubuntu stonking: libmongodb-perl 2.2.2-3 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms