Skip to content

Workload generation

Generate MongoDB find queries for one collection using observed schema values. A pattern fixes fields, operators, types and query options; instances vary the filter values. The generator is synchronous and can be used directly in notebooks.

Generate patterns from observed values

from bondi.mongodb import MongoCollectionRef, MongoGenerationConfig, MongoWorkloadGenerator
from bondi.schema import Schema

schema = Schema.from_documents(
    [{"city": "Sydney", "stars": 4}, {"city": "Melbourne", "stars": 3}],
    values="distinct",
)
generator = MongoWorkloadGenerator(
    target=MongoCollectionRef("demo", "business"),
    schema=schema,
    config=MongoGenerationConfig(min_predicates=1, max_predicates=2),
)
workload, patterns = generator.generate(
    num_patterns=10, instances_per_pattern=50, seed=42, return_patterns=True,
)
workload.save("workload.jsonl")
assert len(workload) == 500
assert patterns[workload[0].pattern_id].matches(workload[0].query)

Omit return_patterns=True to receive only the workload. Set instances_per_pattern=1 for one instance per distinct pattern. Full query instances can repeat; pattern shapes are distinct. Pattern IDs index the returned pattern list. values="full" schemas also work: both modes sample uniformly from distinct values.

Add selectivity constraints

Supply a prepared MongoSampleEstimator as estimator= to the generator, then pass min_selectivity and max_selectivity to generate(). Every accepted instance must meet those bounds on the estimator's sample. The generator does not prepare or close the estimator. See the end-to-end tutorial.

Configure the experiment

Use one MongoGenerationConfig for predicate counts, included/excluded fields, field/operator weights, and sort/projection/skip/limit settings. For example:

config = MongoGenerationConfig(
    min_predicates=1,
    max_predicates=3,
    exclude_fields=["_id", "business_id"],
    sort_probability=0.5,
    projection_probability=0.5,
    limit_probability=0.5,
    limit_min=5,
    limit_max=100,
)

These are proposal settings, not promises about exact output frequencies. A validate_pattern callback can enforce project-specific diversity constraints. Generation returns all requested groups or raises WorkloadGenerationError; it does not silently return a partial workload. Inspect generator.last_stats for rejection counts. Attempt budgets and timeout_seconds bound search, but the deadline cannot interrupt a running estimator call; configure executor timeouts too.

Research boundaries

This generator supports flat find filters, not logical combinations, $elemMatch, joins or aggregation generation. A fixed seed requires fixed schema values and estimator responses for reproducibility. Save the generated workload as the durable experiment input. Sample constraints are not full-population guarantees.

See the generation reference for all options, pattern fields, supported paths and failure diagnostics.