mongoc

MongoDB C driver (libmongoc) bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_mongoc.cC FFI wrapper
c/kit_mongoc.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/mongoc.kitKit Mongoc - MongoDB C driver bindings
tests/mongoc.test.kitTests for mongoc

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-mongoc.git

System Requirements

PlatformCommand
macOSbrew install mongo-c-driver
Ubuntusudo apt install libmongoc-dev
Fedorasudo 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

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This 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