Skip to content

Document Store: API Reference

Document Store: API Reference

Part of: HeliosDB Document Store User Guide

DocumentStore

Core API:

impl DocumentStore {
/// Create a new document store
pub fn new(path: impl AsRef<Path>) -> Result<Self>;
/// Insert a document
pub fn insert(
&self,
collection: &Collection,
id: &DocumentId,
data: serde_json::Value,
) -> Result<Document>;
/// Get a document
pub fn get(
&self,
collection: &Collection,
id: &DocumentId,
) -> Result<Option<Document>>;
/// Update a document
pub fn update(
&self,
collection: &Collection,
id: &DocumentId,
update: serde_json::Value,
) -> Result<Option<Document>>;
/// Delete a document
pub fn delete(
&self,
collection: &Collection,
id: &DocumentId,
) -> Result<bool>;
/// Find documents matching filter
pub fn find(
&self,
collection: &Collection,
filter: Filter,
) -> Result<Vec<Document>>;
/// Count documents
pub fn count(
&self,
collection: &Collection,
filter: Option<Filter>,
) -> Result<u64>;
/// Execute aggregation pipeline
pub fn aggregate(
&self,
collection: &Collection,
pipeline: Vec<AggregationStage>,
) -> Result<Vec<serde_json::Value>>;
/// Create an index
pub fn create_index(&self, definition: IndexDefinition) -> Result<()>;
/// Register schema
pub fn register_schema(
&self,
collection: &str,
schema: serde_json::Value,
) -> Result<()>;
/// Watch collection for changes
pub fn watch(&self, collection: Collection) -> Result<ChangeStream>;
/// Begin transaction
pub fn begin_transaction(
&self,
isolation_level: IsolationLevel,
) -> Result<Arc<Transaction>>;
/// Commit transaction
pub async fn commit_transaction(
&self,
transaction: Arc<Transaction>,
) -> Result<()>;
/// Get collection statistics
pub fn collection_stats(
&self,
collection: &Collection,
) -> Result<Option<CollectionStats>>;
/// List all collections
pub fn list_collections(&self) -> Result<Vec<String>>;
/// Drop a collection
pub fn drop_collection(&self, collection: &Collection) -> Result<()>;
}

MongoDB API Examples

CRUD Operations:

use mongodb::{Client, bson::doc};
// Connect
let client = Client::with_uri_str("mongodb://localhost:27017").await?;
let db = client.database("mydb");
let collection = db.collection("users");
// Insert
collection.insert_one(doc! { "name": "Alice", "age": 30 }, None).await?;
// Find
let cursor = collection.find(doc! { "age": { "$gte": 25 } }, None).await?;
// Update
collection.update_one(
doc! { "name": "Alice" },
doc! { "$set": { "age": 31 } },
None,
).await?;
// Delete
collection.delete_one(doc! { "name": "Alice" }, None).await?;

SQL JSONB Functions

Common Operations:

-- Extract field
SELECT data->>'name' FROM users_doc;
SELECT data->'address'->>'city' FROM users_doc;
-- Filter
SELECT * FROM users_doc WHERE data @> '{"role": "developer"}';
SELECT * FROM users_doc WHERE data->>'age' = '30';
-- Array operations
SELECT * FROM users_doc WHERE data->'skills' ? 'Rust';
SELECT * FROM users_doc WHERE data->'skills' ?| array['Rust', 'MongoDB'];
-- Aggregation
SELECT
data->>'role' AS role,
COUNT(*) AS count
FROM users_doc
GROUP BY data->>'role';

Aggregation Pipeline Examples

Complete Examples:

// Example 1: Sales by region
let pipeline = vec![
doc! { "$match": { "status": "completed" } },
doc! { "$group": {
"_id": "$region",
"totalSales": { "$sum": "$amount" },
"avgOrder": { "$avg": "$amount" },
"count": { "$sum": 1 }
}},
doc! { "$sort": { "totalSales": -1 } },
doc! { "$limit": 10 }
];
// Example 2: User activity analytics
let pipeline = vec![
doc! { "$match": { "eventType": "page_view" } },
doc! { "$group": {
"_id": {
"url": "$page.url",
"date": { "$dateToString": { "format": "%Y-%m-%d", "date": "$timestamp" } }
},
"views": { "$sum": 1 },
"uniqueUsers": { "$addToSet": "$user.id" }
}},
doc! { "$project": {
"url": "$_id.url",
"date": "$_id.date",
"views": 1,
"uniqueUsers": { "$size": "$uniqueUsers" }
}},
doc! { "$sort": { "date": -1, "views": -1 } }
];
// Example 3: Product recommendations
let pipeline = vec![
doc! { "$match": { "userId": "user_123" } },
doc! { "$unwind": "$items" },
doc! { "$group": {
"_id": "$items.category",
"totalSpent": { "$sum": "$items.price" },
"purchases": { "$sum": 1 }
}},
doc! { "$sort": { "totalSpent": -1 } },
doc! { "$limit": 5 }
];


Navigation: ← Previous: Migration from MongoDB | Back to Index