MongoDB · RetrieveMultiple

Set up the service from the database's schema

The service set up from the database instead of by hand, then reading through it, in two scenarios: preload the lead collection at startup, and reading with Strict on, where only the preloaded fields may be named.

Set up for this page

Besides the sample itself, it uses SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Direct.cs (dbContext.Direct); MongoDirectHelper.cs (the checks behind dbContext.Direct), in the files below.

dbContext.Direct is the samples' own checking code on the plain driver, not part of ARX: leave it out of your application.

Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:

Set up
var lead = new ArxEntity("lead");
lead["email"] = "lead-m861@example.com";
lead["full_name"] = "Nuno Teixeira";
lead["score"] = 40;
await service.CreateAsync(lead);

1 Preload at startup

Preload the lead collection once, at startup, and make the service Strict.

Already in scope: connectionString (string)

C#
var catalog = await new ArxPreLoadTable(new ArxPreLoadTableRegister().PreLoadTable("lead")).LoadAsync(connectionString);
var service = new ArxMongoDbService(connectionString, catalog) { Strict = true };

catalog.TryGetTable("lead", out var table);
Console.WriteLine($"lead: id {table.IdColumn}, key {string.Join(", ", table.Keys)}, fields {string.Join(", ", table.Columns.Keys)}");

return service;

Check it in the database

javascript

Run it in mongosh connected to the database, after the code has run.

db.lead.getIndexes()

Expect: _id_ and the unique email_1 index.

2 Read with the preloaded types

Read with the preloaded types: score comes back as an int; a field the preload doesn't know is refused.

Already in scope: service (ArxMongoDbService)

C#
var query = new ArxQueryExpression("lead");
query.ColumnSet.AddColumns("email", "score");
query.Criteria.AddCondition("email", ArxConditionOperator.Equal, "lead-m861@example.com");
var lead = (await service.RetrieveMultipleAsync(query)).Entities.Single();
Console.WriteLine($"score {lead["score"]} ({lead["score"]?.GetType().Name})");

var unknown = new ArxQueryExpression("lead");
unknown.ColumnSet.AddColumn("password");

try
{
    await service.RetrieveMultipleAsync(unknown);
}
catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.NotPreloaded)
{
    Console.WriteLine(exception.Message);
}

Check it in the database

javascript

Run it in mongosh connected to the database, after the code has run.

db.lead.find({ email: "lead-m861@example.com" }, { score: 1 })

Expect: 1 document, score 40.

Good to know

ArxPreLoadTable reads, once, each collection's id, its unique indexes (as keys) and the field types of its $jsonSchema validator. With Strict, a field the preload didn't load is ARX1018 before anything is sent.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.MongoDb --version 1.0.0-rc.1
  4. Database: a MongoDB 7.0, a replica set (tested with 7.0.45, one node) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_MONGODB to its connection string. Nothing is hard-coded.
  6. Source code: replace Program.cs and add the 5 other files below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment and first remakes the sample database from sample-schema.json.

using ArxSamples.MongoDb;
using ArxSamples.MongoDb.Operations.RetrieveMultiple.AdvancedCases;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_MONGODB")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_MONGODB environment variable to your MongoDB connection string.");

var dbContext = new SampleContext(connectionString);

// Every run starts from the sample data: this deletes and makes again what sample-schema.json lists
// (save the file in the project folder, see Get started).
await dbContext.Direct.ResetAsync();

await new Advanced3_DynamicTableSchemaServiceSetup().RunAsync(dbContext);
Advanced3_DynamicTableSchemaServiceSetup.cs

The sample, as it is in the repository.

using ARX.SDK;
using ARX.SDK.MongoDb.Services;
using ARX.SDK.Query;
using ARX.SDK.Services;

using static ArxSamples.MongoDb.SampleOutput;

namespace ArxSamples.MongoDb.Operations.RetrieveMultiple.AdvancedCases;

/// <summary>
/// The service set up from the database instead of by hand, then reading through it, in two scenarios: preload the
/// lead collection at startup, and reading with Strict on, where only the preloaded fields may be named.
/// </summary>
/// <remarks>
/// <c>ArxPreLoadTable</c> reads, once, each collection's id, its unique indexes (as keys) and the field types of its
/// <c>$jsonSchema</c> validator. With <c>Strict</c>, a field the preload didn't load is ARX1018 before anything is sent.
/// </remarks>
public sealed class Advanced3_DynamicTableSchemaServiceSetup : ISample
{
    public string Name => "retrieve-multiple-advanced-3-dynamic-table-schema-service-setup";

    public string Title => "Set up the service from the database's schema";

    public async Task RunAsync(SampleContext dbContext)
    {
        var service = await Scenario1_PreloadAtStartupAsync(dbContext.ConnectionString);

        var lead = new ArxEntity("lead");
        lead["email"] = "lead-m861@example.com";
        lead["full_name"] = "Nuno Teixeira";
        lead["score"] = 40;
        await service.CreateAsync(lead);

        await Scenario2_ReadWithThePreloadedTypesAsync(service);
    }

    /// <summary>1. Preload the lead collection once, at startup, and make the service Strict.</summary>
    private static async Task<ArxMongoDbService> Scenario1_PreloadAtStartupAsync(string connectionString)
    {
        Step("1. Preload at startup");

        var catalog = await new ArxPreLoadTable(new ArxPreLoadTableRegister().PreLoadTable("lead")).LoadAsync(connectionString);
        var service = new ArxMongoDbService(connectionString, catalog) { Strict = true };

        catalog.TryGetTable("lead", out var table);
        Show($"lead: id {table.IdColumn}, key {string.Join(", ", table.Keys)}, fields {string.Join(", ", table.Columns.Keys)}");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * db.lead.getIndexes()
         * -- Expect: _id_ and the unique email_1 index.
         * ===========================================================
         */
        return service;
    }

    /// <summary>2. Read with the preloaded types: score comes back as an int; a field the preload doesn't know is refused.</summary>
    private static async Task Scenario2_ReadWithThePreloadedTypesAsync(ArxMongoDbService service)
    {
        Step("2. Read with the preloaded types");

        var query = new ArxQueryExpression("lead");
        query.ColumnSet.AddColumns("email", "score");
        query.Criteria.AddCondition("email", ArxConditionOperator.Equal, "lead-m861@example.com");
        var lead = (await service.RetrieveMultipleAsync(query)).Entities.Single();
        Show($"score {lead["score"]} ({lead["score"]?.GetType().Name})");

        var unknown = new ArxQueryExpression("lead");
        unknown.ColumnSet.AddColumn("password");

        try
        {
            await service.RetrieveMultipleAsync(unknown);
        }
        catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.NotPreloaded)
        {
            ShowError(exception);
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * db.lead.find({ email: "lead-m861@example.com" }, { score: 1 })
         * -- Expect: 1 document, score 40.
         * ===========================================================
         */
    }
}
SampleContext.cs

What every sample is given: the connection string (and the ISample interface the samples implement).

namespace ArxSamples.MongoDb;

/// <summary>One documentation sample: a scenario a real application has, shown end to end.</summary>
public interface ISample
{
    /// <summary>The name to run it alone: <c>dotnet run -- &lt;name&gt;</c>.</summary>
    string Name { get; }

    /// <summary>One line on what it shows.</summary>
    string Title { get; }

    Task RunAsync(SampleContext dbContext);
}

/// <summary>
/// What a sample is given. Every sample gets the connection string; the application's service and catalog
/// (SampleContext.Application.cs) and the plain MongoDB driver read-back helper (SampleContext.Direct.cs) live in their own
/// files, so a sample needs only the files it uses.
/// </summary>
public sealed partial class SampleContext
{
    public SampleContext(string connectionString)
    {
        ConnectionString = connectionString;
    }

    public string ConnectionString { get; }
}
SampleOutput.cs

How the samples print: Step for each scenario, Show for what it did, ShowError for an error caught on purpose.

using ARX.SDK;

namespace ArxSamples.MongoDb;

/// <summary>How the samples print: a numbered step for each scenario, then what it did.</summary>
public static class SampleOutput
{
    /// <summary>The start of a scenario: <c>Step("1. By the id Create returned")</c>.</summary>
    public static void Step(string text) => Console.WriteLine($"  > {text}");

    /// <summary>What the scenario did.</summary>
    public static void Show(string text) => Console.WriteLine($"      {text}");

    /// <summary>An ARX error the scenario caught on purpose.</summary>
    public static void ShowError(ArxException exception)
        => Console.WriteLine($"      caught {exception.GetType().Name}: {exception.Message}");
}
SampleContext.Direct.cs

The plain MongoDB.Driver read-back helper, for the samples that show what MongoDB stored.

namespace ArxSamples.MongoDb;

/// <summary>The plain MongoDB driver outside the SDK, for the samples that show what MongoDB stored.</summary>
public sealed partial class SampleContext
{
    private MongoDirectHelper? _direct;

    /// <summary>The plain MongoDB driver outside the SDK, used only to reset the database and to show what was stored.</summary>
    public MongoDirectHelper Direct => _direct ??= new MongoDirectHelper(ConnectionString);
}
MongoDirectHelper.cs

Plain MongoDB.Driver, not part of ARX: reads rows back so a sample can show what MongoDB stored.

using MongoDB.Bson;
using MongoDB.Driver;

namespace ArxSamples.MongoDb;

/// <summary>
/// The plain MongoDB driver, outside the SDK: it resets the sample database from Schema/sample-schema.json and shows
/// what was stored. Samples never write through it.
/// </summary>
public sealed class MongoDirectHelper
{
    private readonly IMongoDatabase _database;

    public MongoDirectHelper(string connectionString)
    {
        var url = MongoUrl.Create(connectionString);
        _database = new MongoClient(url).GetDatabase(url.DatabaseName ?? "arx_samples");
    }

    /// <summary>
    /// The sample schema: Schema/sample-schema.json next to the program (the samples' own project), or sample-schema.json
    /// in the project folder (a sample copied from its page, as Get started says).
    /// </summary>
    private static string SchemaPath()
    {
        var candidates = new[]
        {
            Path.Combine(AppContext.BaseDirectory, "Schema", "sample-schema.json"),
            Path.Combine(Directory.GetCurrentDirectory(), "sample-schema.json"),
            Path.Combine(Directory.GetCurrentDirectory(), "Schema", "sample-schema.json")
        };

        return candidates.FirstOrDefault(File.Exists)
            ?? throw new FileNotFoundException("sample-schema.json isn't in the project folder: download it from the Get started page and save it next to Program.cs.");
    }

    /// <summary>Drops the sample database and makes its collections, indexes and validators again.</summary>
    public async Task ResetAsync()
    {
        var schema = BsonDocument.Parse(await File.ReadAllTextAsync(SchemaPath()));

        foreach (var collection in schema["collections"].AsBsonArray.Select(x => x.AsBsonDocument))
        {
            var name = collection["name"].AsString;

            // Only the collections the schema lists are dropped and made again: anything else in the database is left alone.
            await _database.DropCollectionAsync(name);
            var options = new CreateCollectionOptions<BsonDocument>();

            if (collection.TryGetValue("validator", out var validator))
            {
                options.Validator = validator.AsBsonDocument;
            }

            await _database.CreateCollectionAsync(name, options);

            foreach (var index in collection.GetValue("indexes", new BsonArray()).AsBsonArray.Select(x => x.AsBsonDocument))
            {
                var keys = index["keys"].AsBsonDocument;
                var indexOptions = new CreateIndexOptions<BsonDocument> { Unique = index.GetValue("unique", false).AsBoolean };

                if (index.GetValue("partial", false).AsBoolean)
                {
                    indexOptions.PartialFilterExpression = new BsonDocument(keys.Names.Select(x => new BsonElement(x, new BsonDocument("$exists", true))));
                }

                await _database.GetCollection<BsonDocument>(name).Indexes.CreateOneAsync(new CreateIndexModel<BsonDocument>(keys, indexOptions));
            }
        }
    }

    /// <summary>The stored documents a filter finds, as MongoDB's JSON, one line each.</summary>
    public async Task<IReadOnlyList<string>> FindAsync(string collection, string filterJson)
    {
        var documents = await _database.GetCollection<BsonDocument>(collection).Find(BsonDocument.Parse(filterJson)).ToListAsync();
        return documents.Select(x => x.ToJson()).ToList();
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. Preload at startup
      lead: id _id (String, generated), key (email), fields email, full_name, score, captured_on
  > 2. Read with the preloaded types
      score 40 (Int32)
      caught ArxValidationException: ARX1018 NotPreloaded: Column 'password' of 'lead' isn't in the preloaded schema, and the service is Strict.