All examples
Databases

MongoDB synthetic class documents

Append four synthetic BSON documents, find bounded canonical Extended JSON results and inspect a native database command.

db_mongodb.aner
// Public placeholders only. Configure privately: aner secrets set demo-mongodb-password
// This example appends four synthetic documents to a dedicated demo collection.
import aner.db;
import aner.db.mongodb;
import aner.secrets;
import aner.data;

let connection: DBConnection = mongodb.connect(
    host: "db.example.invalid", port: 27017, database: "example_science",
    user: "example_writer", password: secrets.stored("demo-mongodb-password"),
    auth_database: "admin", tls: true, timeout_seconds: 10);

// Native JSON/BSON documents: three numeric attributes and four class labels.
// These fixed examples are authored JSON. Encode untrusted values with a JSON
// serializer in your application; concatenation does not safely escape strings.
print(mongodb.insert_one(connection, "aner_demo_samples",
    "{\"feature_a\":1.2,\"feature_b\":2.3,\"feature_c\":3.4,\"label\":0}"));
print(mongodb.insert_one(connection, "aner_demo_samples",
    "{\"feature_a\":4.2,\"feature_b\":3.3,\"feature_c\":2.4,\"label\":1}"));
print(mongodb.insert_one(connection, "aner_demo_samples",
    "{\"feature_a\":2.2,\"feature_b\":5.3,\"feature_c\":1.4,\"label\":2}"));
print(mongodb.insert_one(connection, "aner_demo_samples",
    "{\"feature_a\":6.2,\"feature_b\":1.3,\"feature_c\":4.4,\"label\":3}"));

let result: DBResult = mongodb.find(connection, "aner_demo_samples",
    "{\"label\":{\"$gte\":0}}",
    options_json: "{\"projection\":{\"_id\":0},\"sort\":{\"label\":1}}",
    max_documents: 1000, max_bytes: 1048576);
print(db.rows(result));
for index in Range(db.rows(result)) {
    print(mongodb.document(result, index));
}
// Each row is one String column named document, preserving BSON type information.
let documents: DataTable = db.to_table(result, 1048576);
print(data.text(documents, 0, "document"));
print(mongodb.command(connection, "{\"ping\":1}", max_bytes: 4096));
db.close(connection);

Make it your experiment.

With Aner installed, save this program as examples/db_mongodb.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/db_mongodb.aner
aner run examples/db_mongodb.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Use a compatible installed Aner executable with the MongoDB C driver and BSON library. Check db.available("mongodb"). Replace the fictional settings privately and store the password with the credential setup commands below using aner.secrets. See the connector and Credentials guides for environment and file references. Configure a trusted certificate authority and keep TLS enabled. This example needs a role permitted to write and read aner_demo_samples in a dedicated development database. Checking opens no connection; running appends four documents on every invocation. Canonical Extended JSON preserves BSON types; DataTable conversion retains document strings and does not create a numeric feature tensor.

Credential setup
aner secrets set demo-mongodb-password --store system
aner secrets status demo-mongodb-password --store system

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner