Collection

Struct Collection 

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

A collection of documents in the database.

Collections are analogous to tables in relational databases and store JSON documents. They provide methods for inserting, querying, updating, and deleting documents.

§Example

use luckdb::{Client, Document};
use serde_json::json;

let mut client = Client::new("mongodb://localhost");
let db = client.db("mydb");
let coll = db.collection("users");

// Insert a document
let user = json!({"name": "Alice", "email": "alice@example.com"});
coll.insert(user).unwrap();

Implementations§

Source§

impl Collection

Source

pub fn new(name: String) -> Self

Creates a new empty collection with the given name.

§Arguments
  • name - The name of the new collection.
Source

pub fn insert(&mut self, doc: Document) -> Result<DocId>

Inserts a single document into the collection, auto-generating an _id field if not present.

§Arguments
  • doc - The document to insert. Must be a JSON object.
§Returns

The ID of the inserted document.

§Errors

Returns an error if the document is not a JSON object.

Source

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

Inserts multiple documents into the collection, auto-generating _id fields for each.

§Arguments
  • docs - A list of documents to insert. Each must be a JSON object.
§Returns

A list of IDs of the inserted documents.

§Errors

Returns an error if any document is not a JSON object.

Source

pub fn find( &self, query: Query, options: Option<FindOptions>, ) -> Result<Vec<(DocId, Document)>>

Finds all documents matching the given query.

§Arguments
  • query - The query to filter documents.
  • options - Optional parameters for sorting, pagination, and projection.
§Returns

A list of tuples containing document IDs and the matching documents.

Source

pub fn find_one( &self, query: Query, options: Option<FindOptions>, ) -> Result<(DocId, Document)>

Finds the first document matching the given query.

§Arguments
  • query - The query to filter documents.
  • options - Optional parameters for sorting and projection.
§Returns

A tuple containing the document ID and the matching document.

§Errors

Returns DbError::NotFound if no document matches the query.

Source

pub fn find_one_and_update( &mut self, query: Query, update: UpdateDocument, options: Option<FindOneAndUpdateOptions>, ) -> Result<Option<(DocId, Document)>>

Finds a document, updates it, and returns the document (either before or after the update).

§Arguments
  • query - The query to filter documents.
  • update - The update operations to apply.
  • options - Optional parameters including upsert behavior and which document version to return.
§Returns

The document before or after the update, depending on options.

Source

pub fn find_one_and_replace( &mut self, query: Query, replacement: Document, options: Option<FindOneAndReplaceOptions>, ) -> Result<Option<(DocId, Document)>>

Source

pub fn find_one_and_delete( &mut self, query: Query, options: Option<FindOneAndDeleteOptions>, ) -> Result<Option<(DocId, Document)>>

Source

pub fn update_one( &mut self, query: Query, update: UpdateDocument, upsert: bool, ) -> Result<usize>

Updates the first document matching the query.

§Arguments
  • query - The query to filter documents.
  • update - The update operations to apply.
  • upsert - If true, creates a new document if no match is found.
§Returns

The number of documents updated (either 0 or 1).

Source

pub fn update_many( &mut self, query: Query, update: UpdateDocument, ) -> Result<usize>

Updates all documents matching the query.

§Arguments
  • query - The query to filter documents.
  • update - The update operations to apply.
§Returns

The number of documents updated.

Source

pub fn replace_one( &mut self, query: Query, replacement: Document, upsert: bool, ) -> Result<usize>

Source

pub fn delete_one(&mut self, query: Query) -> Result<usize>

Deletes the first document matching the query.

§Arguments
  • query - The query to filter documents.
§Returns

The number of documents deleted (either 0 or 1).

Source

pub fn delete_many(&mut self, query: Query) -> Result<usize>

Deletes all documents matching the query.

§Arguments
  • query - The query to filter documents.
§Returns

The number of documents deleted.

Source

pub fn count_documents(&self, query: Query) -> Result<usize>

Counts the number of documents matching the query.

§Arguments
  • query - The query to filter documents.
§Returns

The number of matching documents.

Source

pub fn estimated_document_count(&self) -> Result<usize>

Returns the estimated count of documents in the collection.

§Returns

The total number of documents in the collection.

Source

pub fn create_index(&mut self, index: Index) -> Result<()>

Creates an index on the collection.

§Arguments
  • index - The index definition containing field, type, uniqueness, and other properties.
§Errors

Returns an error if an index with the same name already exists or if there’s a problem creating the index.

Source

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

Drops an existing index from the collection.

§Arguments
  • name - The name of the index to drop.
Source

pub fn list_indexes(&self) -> Result<Vec<Index>>

Lists all indexes created on the collection.

§Returns

A list of index definitions.

Source

pub fn drop_indexes(&mut self) -> Result<()>

Drops all indexes from the collection.

Source

pub fn stats(&self) -> Result<CollectionStats>

Source

pub fn aggregate( &self, pipeline: Vec<AggregationStage>, ) -> Result<Vec<Document>>

Source

pub fn distinct(&self, field: &str, query: Option<Query>) -> Result<Vec<Value>>

Source

pub fn count(&self) -> usize

Source§

impl Collection

Source

pub fn bulk_write( &mut self, operations: Vec<BulkWriteOperation>, options: Option<BulkWriteOptions>, ) -> Result<BulkWriteResult>

Trait Implementations§

Source§

impl Clone for Collection

Source§

fn clone(&self) -> Collection

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Collection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Collection

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Collection> for PersistentCollection

Source§

fn from(collection: Collection) -> Self

Converts to this type from the input type.
Source§

impl From<PersistentCollection> for Collection

Source§

fn from(persistent: PersistentCollection) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Collection

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,