MongoDB

Get started

Everything the samples need on your machine, once: the .NET SDK, a project, the packages, the database and the connection string.

  1. Install the .NET SDK

    .NET SDK 10.0.100 or later 10.0.x. The samples target net10.0. Check what you have:

    Terminal
    dotnet --version
  2. Create a project

    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Add the packages

    • ARX.SDK.MongoDb1.0.0-rc.1: The MongoDB provider. It brings ARX.SDK and MongoDB.Driver 3.13.0 with it.
    • ARX.SDK1.0.0-rc.1: The core: entities, queries, the schema catalog, errors. Comes with the provider.
    • MongoDB.Driver3.13.0: The MongoDB C# driver. Comes with the provider.
    Terminal
    dotnet add package ARX.SDK.MongoDb --version 1.0.0-rc.1

    Some advanced samples need one more package; their pages give the exact command.

  4. Create the database and the sample schema

    MongoDB 7.0, a replica set (tested with 7.0.45, one node), through MongoDB.Driver 3.13.0 or later (the lowest version, which the provider is built on; Versions and compatibility lists the versions it is tested on). With Docker, start one and create the database (choose your own password):

    Terminal
    docker run --name arx-samples-mongo -p 27017:27017 -d mongo:7.0 --replSet rs0 --bind_ip_all
    docker exec arx-samples-mongo mongosh --quiet --eval "rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] })"

    Then save sample-schema.json (download it, or copy it below) in your project folder, next to Program.cs. You don't run it yourself: the Program.cs on every sample page deletes and makes again what it lists before the sample runs.

    sample-schema.json

    The sample schema the samples run against.

    {
      "comment": "The sample database, as the samples' reset makes it: each collection with its unique indexes (a partial one only indexes documents that have the field) and, where ARX's preload reads one, a $jsonSchema validator.",
      "collections": [
        { "name": "account", "indexes": [ { "keys": { "account_number": 1 }, "unique": true, "partial": true } ] },
        { "name": "contact", "indexes": [ { "keys": { "email": 1 }, "unique": true, "partial": true }, { "keys": { "account_id": 1 } } ] },
        { "name": "country" },
        { "name": "product", "indexes": [ { "keys": { "name": "text", "description": "text" } } ] },
        { "name": "price_list", "indexes": [ { "keys": { "region": 1, "sku": 1 }, "unique": true } ] },
        { "name": "sales_order", "indexes": [ { "keys": { "order_number": 1 }, "unique": true, "partial": true } ] },
        { "name": "activity_log" },
        { "name": "legacy_customer" },
        {
          "name": "lead",
          "indexes": [ { "keys": { "email": 1 }, "unique": true } ],
          "validator": {
            "$jsonSchema": {
              "bsonType": "object",
              "required": [ "email" ],
              "properties": {
                "_id": { "bsonType": "objectId" },
                "email": { "bsonType": "string" },
                "full_name": { "bsonType": "string" },
                "score": { "bsonType": [ "int", "null" ] },
                "captured_on": { "bsonType": "date" }
              }
            }
          }
        }
      ]
    }
    
  5. Configure the connection

    The samples read the connection string from the ARX_SAMPLES_MONGODB environment variable, never from the code. Set it in the terminal you run them from (with your password, where the database has one):

    Terminal

    macOS and Linux (bash, zsh)

    export ARX_SAMPLES_MONGODB="mongodb://localhost:27017/arx_samples?replicaSet=rs0"
    PowerShell

    Windows PowerShell

    $env:ARX_SAMPLES_MONGODB = "mongodb://localhost:27017/arx_samples?replicaSet=rs0"
  6. Add a sample and run it

    Each sample page lists its files: copy each into the project with the file name shown, then run:

    Terminal
    dotnet run

    The page shows the output to expect. Each run starts from the sample data: the page's Program.cs remakes the sample database from sample-schema.json first, so a sample can run again and again.