Skip to content

Collection API Reference ​

The Collection struct provides CRUD operations, indexing, and aggregation for a group of documents.

Installation ​

Add PocketDB to your Cargo.toml:

toml
[dependencies]
pocketdb = "0.1"

Or via the command line:

bash
cargo add pocketdb

Import ​

rust
use pocketdb::collection::Collection;

Collections are obtained from Database::collection():

rust
let col = db.collection("users")?;

Methods ​

Collection::name ​

rust
pub fn name(&self) -> &str

Returns the collection name.


Collection::insert ​

rust
pub fn insert(&self, doc: Document) -> Result<ObjectId>

Inserts a single document, returning its generated _id.

Parameters:

  • doc — The document to insert

Returns: Result<ObjectId> — The inserted document's ID

Example:

rust
use pocketdb::doc;

let id = col.insert(doc! { "name" => "Alice", "age" => 30 })?;

Collection::insert_many ​

rust
pub fn insert_many(&self, docs: Vec<Document>) -> Result<Vec<ObjectId>>

Inserts multiple documents, returning their IDs.

Parameters:

  • docs — Vector of documents to insert

Returns: Result<Vec<ObjectId>>


Collection::find ​

rust
pub fn find(&self, filter: Filter) -> QueryBuilder

Returns a QueryBuilder for constructing and executing queries.

Parameters:

  • filter — The filter expression

Returns: QueryBuilder

Example:

rust
use pocketdb::filter;

let results = col.find(filter! { "age" => > 18 })
    .sort("age", SortDirection::Asc)
    .limit(10)
    .run(&col.all_documents())?;

Collection::find_one ​

rust
pub fn find_one(&self, filter: &Filter) -> Option<Document>

Finds the first document matching the filter.

Parameters:

  • filter — The filter expression

Returns: Option<Document>


Collection::update_one ​

rust
pub fn update_one(&self, filter: &Filter, update: &UpdateExpression) -> Result<bool>

Updates the first document matching the filter.

Parameters:

  • filter — The filter expression
  • update — The update expression

Returns: Result<bool> — true if a document was updated


Collection::update_many ​

rust
pub fn update_many(&self, filter: &Filter, update: &UpdateExpression) -> Result<usize>

Updates all documents matching the filter.

Returns: Result<usize> — Number of documents updated


Collection::replace_one ​

rust
pub fn replace_one(&self, filter: &Filter, replacement: &Document) -> Result<bool>

Replaces the first matching document's body, keeping its _id.

Returns: Result<bool> — true if a document was replaced


Collection::upsert ​

rust
pub fn upsert(&self, filter: &Filter, doc: &Document) -> Result<ObjectId>

Updates if a match exists, otherwise inserts.

Returns: Result<ObjectId> — The document's ID


Collection::delete_one ​

rust
pub fn delete_one(&self, filter: &Filter) -> Result<bool>

Deletes the first document matching the filter.

Returns: Result<bool> — true if a document was deleted


Collection::delete_many ​

rust
pub fn delete_many(&self, filter: &Filter) -> Result<usize>

Deletes all documents matching the filter.

Returns: Result<usize> — Number of documents deleted


Collection::count ​

rust
pub fn count(&self, filter: &Filter) -> Result<usize>

Counts documents matching the filter.

Returns: Result<usize>


Collection::distinct ​

rust
pub fn distinct(&self, field: &str, filter: &Filter) -> Result<Vec<PocketValue>>

Returns distinct values of a field across matching documents.

Returns: Result<Vec<PocketValue>>


Collection::bulk_write ​

rust
pub fn bulk_write(&self, ops: Vec<BulkOp>) -> Result<usize>

Executes a batch of mixed operations atomically.

BulkOp variants:

  • BulkOp::Insert(Document)
  • BulkOp::UpdateOne(Filter, UpdateExpression)
  • BulkOp::UpdateMany(Filter, UpdateExpression)
  • BulkOp::DeleteOne(Filter)
  • BulkOp::DeleteMany(Filter)
  • BulkOp::ReplaceOne(Filter, Document)

Returns: Result<usize> — Number of affected documents


Collection::create_index ​

rust
pub fn create_index(&self, spec: IndexSpec) -> Result<()>

Creates an index on this collection.


Collection::drop_index ​

rust
pub fn drop_index(&self, name: &str) -> Result<()>

Drops an index by name.


Collection::list_indexes ​

rust
pub fn list_indexes(&self) -> Vec<String>

Returns the names of all indexes on this collection.


Collection::explain ​

rust
pub fn explain(&self, filter: &Filter) -> ExplainPlan

Returns the query plan for a filter, showing which index will be used.


Collection::aggregate ​

rust
pub fn aggregate(&self) -> AggregationPipeline

Returns an aggregation pipeline for this collection.


Collection::all_documents ​

rust
pub fn all_documents(&self) -> Vec<Document>

Returns all documents in the collection.


Collection::len ​

rust
pub fn len(&self) -> usize

Returns the document count.


Collection::is_empty ​

rust
pub fn is_empty(&self) -> bool

Returns whether the collection is empty.

Released under the MIT License. Built by Muhammad Fiaz.