# mongkn

> MongoDB where no official driver exists. A binding over libmongoc behind the shape of the coroutine driver's API, with read overhead indistinguishable from bare C.

Page: https://kotlin.website/mongkn

## Why it exists

There is no official MongoDB driver for Kotlin/Native — `mongodb-driver-kotlin-coroutine` is
JVM-only. mongkn writes the cinterop once and hides it behind `suspend fun insertOne(…)` and
`fun find(…): Flow<Document>`.

## What you get

- **24 of 30 collection operations**: insert, update, delete, `find`, aggregation, indexes,
  `bulkWrite`, `findOneAnd*`, `distinct`. The six missing are missing on purpose — five are
  Atlas Search index operations, which do not exist on a self-hosted server, and the sixth is
  `mapReduce`, deprecated by MongoDB itself.
- **Sessions and transactions**, with `withTransaction` retrying on server error labels.
- **Change streams** on collection, database and client, with automatic resumption.
- All topologies: standalone, replica set, sharded through `mongos`. SCRAM, TLS and x509,
  verified against servers started with `--auth` and `--tlsMode requireTLS`.
- **18 of 20 BSON types**, its own tree model, and a `kotlinx.serialization` format on top.

## Performance

Overhead over the C driver it wraps, from a release binary against a local `mongo:8`:

| | |
|---|---|
| Write, per operation | 1470 µs against 1492 µs for bare C — below the measurement threshold |
| Read into a class, per document | 1.26 µs on Linux/x86_64, 1.66 µs on macOS/arm64 |
| Read overhead over a bare C cursor loop | −0.35 %, indistinguishable |
| `kotlinx.serialization` codec | 0.27 µs, about 5 % of the mandatory conversion |

## Install

```kotlin
repositories { maven("https://reposilite.kotlin.website/snapshots") }
dependencies { implementation("io.github.youndie.mongkn:mongkn-core:<VERSION>") }
```

The write-up above is the one worth reading before you depend on this: four bugs that a full
test suite and 71 specification scenarios did not catch, because none of them threw.
