A recurring problem in multi-service backends is storing user-defined filter rules in a database (feature flags, webhook routing, promotion eligibility, alert filters) and then evaluating those same rules in memory across different runtimes.
One service is in Ruby, a worker is in Go or Python, and the admin UI previews the rule in TypeScript before saving it. Without a shared matcher, teams either invent a custom JSON DSL and maintain three slightly different interpreters, or spin up a database round-trip for data that is already sitting in RAM.
Most developers already know MongoDB's query syntax ($and, $or, $in, $elemMatch, dot paths). It serializes cleanly as plain JSON and does not require eval.
That is why I built Mongory, an in-memory MongoDB-style query and rule engine implemented across five languages:
- mongory-core (C99, zero external dependencies)
-
mongory-rb (Ruby 2.6-4.0, C extension wrapping
mongory-core) -
mongory-py (Python 3.10-3.14, C-API extension wrapping
mongory-core) - mongory-go (Pure Go, zero dependencies)
- mongory-js (Pure TypeScript/JS, zero dependencies, runs in Node, Bun, Deno, and browsers)
You can test rules in the browser right away in the Mongory Playground.
One JSON rule across every service
Take a rule stored in a database or sent from a frontend form:
{
"age": { "$gte": 18 },
"status": { "$in": ["active", "trial"] },
"tags": { "$elemMatch": { "name": "pro", "score": { "$gt": 80 } } }
}
Compile it once, then match records in memory.
In Ruby:
require 'json'
require 'mongory'
rule = JSON.parse(raw_json)
matcher = Mongory.compile(rule)
eligible = users.select(&matcher)
In Python:
import json
import mongory
rule = json.loads(raw_json)
matcher = mongory.compile(rule)
eligible = list(matcher.filter(users))
In Go (matching maps or structs directly via mongory, bson, or json tags):
import (
"encoding/json"
"github.com/mongoryhq/mongory-go"
)
var rule mongory.M
if err := json.Unmarshal(rawJSON, &rule); err != nil {
return err
}
matcher, err := mongory.Compile(rule)
if err != nil {
return err
}
eligible := mongory.Filter(users, matcher)
In TypeScript:
import { mongory } from "mongory-js";
const rule = JSON.parse(rawJson);
const matcher = mongory.compile(rule);
const eligible = users.filter(matcher.match);
Debugging rules with explain and trace trees
Complex nested $and / $or / $elemMatch rules fail silently when a boolean matcher just returns false. Every Mongory implementation can print the compiled matcher tree (explain) and evaluate a specific record with per-node PASS / FAIL / SKIP status (trace). For example, from mongory-go:
ok, trace := m.Trace(mongory.M{"age": 15, "status": "active"})
fmt.Print(trace)
[FAIL] And | value={"age": 15, "status": "active"}
├── [PASS] Field("status") | value="active"
│ └── [PASS] Eq("active") | value="active"
└── [FAIL] Field("age") | value=15
└── [FAIL] Gte(18) | value=15
Having the same trace tree in the browser playground and in backend logs makes it much easier to answer "why didn't this user match the rule?" without stepping through code.
Why C99 for Ruby/Python, and pure native for Go/JS
I did not force every language through FFI. Each runtime has different trade-offs:
- Ruby and Python have high interpreter overhead for recursive tree walking, and both ship precompiled native extensions cleanly (8 platform gems on RubyGems, manylinux/macOS/Windows wheels on PyPI). Wrapping one shared C99 core (
mongory-core) keeps both bindings fast and avoids duplicating matcher logic. - Go has cgo overhead that slows down small per-record calls and breaks cross-compilation. A pure Go engine with zero dependencies compiles anywhere and runs at
0 allocs/opon map and struct hot paths (~25 ns/recon maps,~35 ns/recon structs). - JavaScript needs to run inside browsers, Cloudflare Workers, Deno, Bun, and Node without WASM startup cost or native build toolchains, so pure TypeScript was the right fit.
Keeping five implementations consistent
Porting a query engine five times guarantees subtle drift unless you lock down edge cases early. All five repositories run CI against a single shared test suite, mongory-spec.
The spec pins down the details that usually diverge across libraries:
-
nullfield value vs missing key ($exists) - Implicit array element matching (
{ "scores": { "$gt": 80 } }) vs$elemMatch - Strict boolean vs integer separation (
true != 1in Python and C) - Escaped dots in field names (
"user\\.name"vs nested"user.name") - Numeric equivalence across int and float (
1 == 1.0,-0.0 == 0,NaNhandling)
Benchmark snapshot
All numbers below are from local runs on an Apple M3 (macOS arm64). Each benchmark compares compiled matchers against existing in-memory MongoDB query libraries in the same language, including a 500-item $in query where Mongory builds an O(1) hash lookup table at compile time.
| Language | Comparison target | Standard queries | 500-item $in
|
|---|---|---|---|
Ruby (mongory-rb v2.1.1) |
Mongoid::Matcher 9.1.2 |
31x - 41x faster | 840x faster |
Python (mongory-py v0.2.1) |
mongoquery 1.4.3 |
9.2x - 24x faster | 2,190x - 2,310x faster |
Go (mongory-go v0.4.0) |
lungo/mongokit / connor
|
1.1x - 5.3x faster (0 allocs/op) | 88x - 172x faster |
JS (mongory-js v0.2.0) |
sift / mingo
|
1.5x - 6.9x faster | 200x - 350x faster |
Full benchmark scripts and raw tables live in each repository's docs/performance.md.
When not to use Mongory
Mongory only matches records already in memory. Skip it if:
- Your dataset does not fit in memory or you need secondary indexes, persistence, or aggregation pipelines (
$group,$lookup). Use a real database. - You need a mock MongoDB wire-protocol server for integration tests. Use a real MongoDB container or Testcontainers instead.
- You only have one hardcoded
ifcheck in a single language. Plain native code is simpler and always slightly faster than interpreting a rule object.
Links
- Browser Playground and docs hub: https://mongoryhq.github.io/mongory-central/
- GitHub organization (all 5 engines +
mongory-spec): https://github.com/mongoryhq
If you have run into cross-language rule evaluation or tricky MongoDB query edge cases, I would love to hear how you handled them.

Top comments (0)