Struct mongodb::sync::SessionCursor
source · [−]pub struct SessionCursor<T> { /* private fields */ }
sync
or tokio-sync
only.Expand description
A SessionCursor
is a cursor that was created with a ClientSession
must be iterated using
one. To iterate, retrieve a [SessionCursorIter]
using SessionCursor::iter
:
for doc in cursor.iter(&mut session) {
println!("{}", doc?)
}
Implementations
sourceimpl<T> SessionCursor<T>
impl<T> SessionCursor<T>
sourcepub fn advance(&mut self, session: &mut ClientSession) -> Result<bool>
pub fn advance(&mut self, session: &mut ClientSession) -> Result<bool>
Move the cursor forward, potentially triggering requests to the database for more results if the local buffer has been exhausted.
This will keep requesting data from the server until either the cursor is exhausted or batch with results in it has been received.
The return value indicates whether new results were successfully returned (true) or if the cursor has been closed (false).
Note: Cursor::current
and Cursor::deserialize_current
must only be called after
Cursor::advance
returned Ok(true)
. It is an error to call either of them without
calling Cursor::advance
first or after Cursor::advance
returns an error / false.
let mut cursor = coll.find_with_session(None, None, &mut session)?;
while cursor.advance(&mut session)? {
println!("{:?}", cursor.deserialize_current()?);
}
sourcepub fn current(&self) -> &RawDocument
pub fn current(&self) -> &RawDocument
Returns a reference to the current result in the cursor.
Panics
Cursor::advance
must return Ok(true)
before Cursor::current
can be
invoked. Calling Cursor::current
after Cursor::advance
does not return true
or without calling Cursor::advance
at all may result in a panic.
let mut cursor = coll.find_with_session(None, None, &mut session)?;
while cursor.advance(&mut session)? {
println!("{:?}", cursor.current());
}
sourcepub fn deserialize_current<'a>(&'a self) -> Result<T> where
T: Deserialize<'a>,
pub fn deserialize_current<'a>(&'a self) -> Result<T> where
T: Deserialize<'a>,
Deserialize the current result to the generic type associated with this cursor.
Panics
Cursor::advance
must return Ok(true)
before Cursor::deserialize_current
can be
invoked. Calling Cursor::deserialize_current
after Cursor::advance
does not return
true or without calling Cursor::advance
at all may result in a panic.
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Cat<'a> {
#[serde(borrow)]
name: &'a str
}
let coll = db.collection::<Cat>("cat");
let mut cursor = coll.find_with_session(None, None, &mut session)?;
while cursor.advance(&mut session)? {
println!("{:?}", cursor.deserialize_current()?);
}
sourceimpl<T> SessionCursor<T> where
T: DeserializeOwned + Unpin + Send + Sync,
impl<T> SessionCursor<T> where
T: DeserializeOwned + Unpin + Send + Sync,
sourcepub fn iter<'session>(
&mut self,
session: &'session mut ClientSession
) -> SessionCursorIter<'_, 'session, T>ⓘNotable traits for SessionCursorIter<'_, '_, T>impl<T> Iterator for SessionCursorIter<'_, '_, T> where
T: DeserializeOwned + Unpin + Send + Sync, type Item = Result<T>;
pub fn iter<'session>(
&mut self,
session: &'session mut ClientSession
) -> SessionCursorIter<'_, 'session, T>ⓘNotable traits for SessionCursorIter<'_, '_, T>impl<T> Iterator for SessionCursorIter<'_, '_, T> where
T: DeserializeOwned + Unpin + Send + Sync, type Item = Result<T>;
T: DeserializeOwned + Unpin + Send + Sync, type Item = Result<T>;
Retrieves a SessionCursorIter
to iterate this cursor. The session provided must be
the same session used to create the cursor.
sourcepub fn next(&mut self, session: &mut ClientSession) -> Option<Result<T>>
pub fn next(&mut self, session: &mut ClientSession) -> Option<Result<T>>
Retrieve the next result from the cursor. The session provided must be the same session used to create the cursor.
Use this method when the session needs to be used again between iterations or when the added
functionality of Iterator
is not needed.
let mut cursor = coll.find_with_session(doc! { "x": 1 }, None, &mut session)?;
while let Some(doc) = cursor.next(&mut session).transpose()? {
other_coll.insert_one_with_session(doc, None, &mut session)?;
}
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for SessionCursor<T>
impl<T> Send for SessionCursor<T> where
T: Send,
impl<T> Sync for SessionCursor<T> where
T: Sync,
impl<T> Unpin for SessionCursor<T> where
T: Unpin,
impl<T> !UnwindSafe for SessionCursor<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more