mongoc
| Kind | ffi-c |
|---|---|
| Capabilities | ffi |
| Categories | database ffi |
| Keywords | mongodb mongo nosql database bson |
MongoDB C driver (libmongoc) bindings for Kit
Files
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
c/kit_mongoc.c | C FFI wrapper |
c/kit_mongoc.h | C header for FFI wrapper |
examples/basic.kit | Basic usage example |
kit.toml | Package manifest with metadata and dependencies |
src/mongoc.kit | Kit Mongoc - MongoDB C driver bindings |
tests/mongoc.test.kit | Tests for mongoc |
Dependencies
No Kit package dependencies.
Installation
kit add gitlab.com/kit-lang/packages/kit-mongoc.gitSystem Requirements
| Platform | Command |
|---|---|
| macOS | brew install mongo-c-driver |
| Ubuntu | sudo apt install libmongoc-dev |
| Fedora | sudo dnf install mongo-c-driver-devel |
Usage
import Kit.MongoC as Mongoc
main = fn =>
Mongoc.init
match Mongoc.client-new "mongodb://localhost:27017"
| Ok client ->
# Ping the server
match Mongoc.ping client
| Ok _ -> println "Connected to MongoDB"
| Err e -> println ("Ping failed: " ++ e)
match Mongoc.get-collection client "mydb" "users"
| Ok coll ->
# Insert a document
match Mongoc.insert coll "{\"name\": \"Alice\", \"age\": 30}"
| Ok _ -> println "Inserted"
| Err e -> println ("Insert error: " ++ e)
# Find documents
docs = Mongoc.find-all coll
println ("All documents: " ++ docs)
# Find with filter
filtered = Mongoc.find coll "{\"age\": {\"$gt\": 25}}"
println ("Filtered: " ++ filtered)
# Find one
match Mongoc.find-one coll "{\"name\": \"Alice\"}"
| Some doc -> println ("Found: " ++ doc)
| None -> println "Not found"
# Count
match Mongoc.count coll
| Ok n -> println ("Count: " ++ (show n))
| Err e -> println ("Count error: " ++ e)
Mongoc.collection-destroy coll
| Err e -> println ("Collection error: " ++ e)
Mongoc.client-destroy client
| Err e -> println ("Connection error: " ++ e)
Mongoc.cleanup
mainDevelopment
Running Examples
Run examples with the interpreter:
kit run examples/basic.kitCompile examples to a native binary:
kit build examples/basic.kit && ./basicRunning Tests
Run the test suite:
kit testRun the test suite with coverage:
kit test --coverageRunning kit dev
Run the standard development workflow (format, check, test):
kit devThis will:
- Format and check source files in
src/ - Run tests in
tests/with coverage
Generating Documentation
Generate API documentation from doc comments:
kit docNote: Kit sources with doc comments (##) will generate HTML documents in docs/*.html
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task cleanNote: Defined in kit.toml.
Local Installation
To install this package locally for development:
kit installThis installs the package to ~/.kit/packages/@kit/mongoc/, making it available for import as Kit.MongoC in other projects.
License
This package is released under the MIT License - see LICENSE for details.
libmongoc is released under the Apache License 2.0.
Exported Functions & Types
init
Initialize the MongoDB driver. Must be called before any other operations.
Unit
cleanup
Clean up the MongoDB driver and release global resources.
Unit
client-new
Create a new MongoDB client from a connection URI.
Parameters:
Returns:
String -> Result Ptr String
client-destroy
Destroy a MongoDB client and free its resources.
Parameters:
Ptr -> Unit
ping
Ping the MongoDB server to check connectivity.
Parameters:
Returns:
Ptr -> Result Unit String
get-database
Get a database handle.
Parameters:
Returns:
Ptr -> String -> Result Ptr String
database-destroy
Destroy a database handle.
Parameters:
Ptr -> Unit
list-collections
List collection names in a database as a comma-separated string.
Parameters:
Returns:
Ptr -> String
get-collection
Get a collection handle.
Parameters:
Returns:
Ptr -> String -> String -> Result Ptr String
collection-destroy
Destroy a collection handle.
Parameters:
Ptr -> Unit
count
Count documents in a collection.
Parameters:
Returns:
Ptr -> Result Int String
insert
Insert a JSON document into a collection.
Parameters:
Returns:
Ptr -> String -> Result Unit String
find
Find documents matching a filter. Returns a JSON array string.
Parameters:
Returns:
Ptr -> String -> String
find-all
Find all documents in a collection. Returns a JSON array string.
Parameters:
Returns:
Ptr -> String
find-one
Find a single document matching a filter. Returns a JSON string.
Parameters:
Returns:
Ptr -> String -> Option String
update-one
Update a single document matching a filter.
Parameters:
Returns:
Ptr -> String -> String -> Result Unit String
delete-one
Delete a single document matching a filter.
Parameters:
Returns:
Ptr -> String -> Result Unit String
delete-many
Delete all documents matching a filter.
Parameters:
Returns:
Ptr -> String -> Result Unit String
drop
Drop (delete) an entire collection.
Parameters:
Returns:
Ptr -> Result Unit String
create-index
Create an index on a collection.
Parameters:
Returns:
Ptr -> String -> Result Unit String