MongoDB with the C# driver
Uses MongoDB.Driver 3.13 on .NET 10. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.
The only change
Section titled “The only change”mongodb://app:secret@mongo.example.com:27017/shop?authSource=adminmongodb://app:secret@machnosql.example.com:27017/shop?authSource=adminThe address comes from the environment variable MONGODB_URI, so switching to MachNoSQL changes configuration only.
export MONGODB_URI='mongodb://app:secret@machnosql.example.com:27017/shop?authSource=admin'dotnet runSource
Section titled “Source”The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.
// Orders example for MongoDB, unchanged on MachNoSQL.using MongoDB.Bson;using MongoDB.Driver;
var client = new MongoClient(Environment.GetEnvironmentVariable("MONGODB_URI"));var orders = client.GetDatabase("shop").GetCollection<BsonDocument>("orders");
await orders.DeleteManyAsync(FilterDefinition<BsonDocument>.Empty);await orders.InsertManyAsync(new[]{ new BsonDocument { { "customer", "ada" }, { "item", "keyboard" }, { "qty", 1 }, { "price", 49.0 } }, new BsonDocument { { "customer", "ada" }, { "item", "mouse" }, { "qty", 2 }, { "price", 19.5 } }, new BsonDocument { { "customer", "linus" }, { "item", "monitor" }, { "qty", 1 }, { "price", 199.0 } },});
var byItem = Builders<BsonDocument>.Filter.Eq("item", "mouse");Console.WriteLine(await orders.Find(byItem).FirstOrDefaultAsync());await orders.UpdateOneAsync(byItem, Builders<BsonDocument>.Update.Inc("qty", 1));
var pipeline = new[]{ BsonDocument.Parse("{ $group: { _id: '$customer', revenue: { $sum: { $multiply: ['$qty', '$price'] } } } }"), BsonDocument.Parse("{ $sort: { revenue: -1 } }"),};foreach (var row in await orders.Aggregate<BsonDocument>(pipeline).ToListAsync()){ Console.WriteLine(row);}<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="MongoDB.Driver" Version="3.13.0" /> </ItemGroup></Project>