Skip to main content

Collection

Struct Collection 

Source
pub struct Collection { /* private fields */ }
Expand description

A handle to a named document collection inside a Database.

Provides MongoDB-compatible methods: insert_one, find_one, find, update_one, update_many, delete_one, delete_many, count_documents.

Implementations§

Source§

impl Collection

Source

pub fn name(&self) -> &str

The collection name.

Source

pub fn insert_one(&self, document: Value) -> Result<InsertOneResult>

Insert a single document and return its assigned ID.

let coll = client.database().collection("users");
let result = coll.insert_one(json!({"name": "Alice", "age": 30})).unwrap();
println!("inserted: {}", result.inserted_id);
Source

pub fn insert_many(&self, documents: Vec<Value>) -> Result<InsertManyResult>

Insert multiple documents and return all assigned IDs.

Source

pub fn find_one(&self, filter: Option<&Value>) -> Result<Option<Value>>

Find a single document matching filter, or None if not found.

Supports {"_id": "..."} for fast lookup as well as arbitrary MongoDB-style operators.

Source

pub fn find(&self, filter: Option<&Value>) -> Result<Cursor>

Return a Cursor over all documents that match filter.

Pass None to return every document in the collection.

Source

pub fn update_one(&self, filter: &Value, update: &Value) -> Result<UpdateResult>

Update the first document that matches filter using update operators.

Supports $set, $unset, $inc, $push, and full replacement.

Source

pub fn update_many( &self, filter: &Value, update: &Value, ) -> Result<UpdateResult>

Update all documents that match filter.

Source

pub fn delete_one(&self, filter: &Value) -> Result<DeleteResult>

Delete the first document that matches filter.

Source

pub fn delete_many(&self, filter: &Value) -> Result<DeleteResult>

Delete all documents that match filter.

Source

pub fn count_documents(&self, filter: Option<&Value>) -> Result<usize>

Count documents matching filter, or all documents when None.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.