Skip to content

CXX-2659 Add change_stream::next()#1594

Merged
eramongodb merged 3 commits intomongodb:masterfrom
eramongodb:cxx-2659
Mar 12, 2026
Merged

CXX-2659 Add change_stream::next()#1594
eramongodb merged 3 commits intomongodb:masterfrom
eramongodb:cxx-2659

Conversation

@eramongodb
Copy link
Contributor

Resolves CXX-2659 by exposing the advance_iterator() internal helper as a public .next() function. As suggested by CXX-2659, the function returns the event document. However, rather than throwing an exception, the return value is made an optional instead, where cs.next().has_value() is effectively equivalent to (++iter) != cs.end(). The existing change streams iteration test case is updated to reflect this equivalence between cs.next() and ++iter, including the fact that .next() followed by .begin() does not advance the underlying cursor (the reverse order is not true).

@eramongodb eramongodb requested a review from kevinAlbs March 11, 2026 19:34
@eramongodb eramongodb self-assigned this Mar 11, 2026
@eramongodb eramongodb requested a review from a team as a code owner March 11, 2026 19:34
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM with a rename to try_next.

@eramongodb eramongodb requested a review from kevinAlbs March 11, 2026 20:59
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a documentation tweak. I am very happy to see this simplify common use of the change stream API.

///
/// Try to obtain the next event document.
///
/// This function does not block: the underlying cursor is advanced only once.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Advancing the cursor may block (for up to maxAwaitTimeMS). try_next gives users an option to exit early and/or save the current resume token.

Suggested change
/// This function does not block: the underlying cursor is advanced only once.
/// This function does not block indefinitely: the underlying cursor is advanced only once.

@eramongodb eramongodb merged commit 5362df0 into mongodb:master Mar 12, 2026
2 of 3 checks passed
@eramongodb eramongodb deleted the cxx-2659 branch March 12, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants