MongoInteractions 1.0.0

dotnet add package MongoInteractions --version 1.0.0
NuGet\Install-Package MongoInteractions -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MongoInteractions" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoInteractions --version 1.0.0
#r "nuget: MongoInteractions, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MongoInteractions as a Cake Addin
#addin nuget:?package=MongoInteractions&version=1.0.0

// Install MongoInteractions as a Cake Tool
#tool nuget:?package=MongoInteractions&version=1.0.0

MongoInteractions

The easiest way to work with a MongoDB cluster.

Table of Contents

Package Support

Framework Is Supported? Extra Info
.NET 6 ☑️
.NET 7 ☑️
.NET Framework May never be supported due to storage limitations on this laptop.
.NET Core May never be supported due to storage limitations on this laptop.
Mono Not supported yet, but Mono support is planned.
Unity Will NEVER be supported, along with other C# game engines.

Installation

Package Manager Console (Visual Studio)

Install-Package MongoInteractions

.NET CLI

dotnet add package MongoInteractions

Usage

This example uses .NET 7, but this package supports .NET 6 as well.

Note

This example uses a .env file which is recommended if your MongoDB project has the 0.0.0.0/0 IP set in the Network Access page.

using DotNetEnv;
using MongoDB.Driver;
using MongoInteractions;
using TestProject.Net7.Models;

namespace TestProject.Net7
{
    public class Program
    {
        // Asynchronous usage
        public static void Main() => new Program().MainAsync().GetAwaiter().GetResult();

        public async Task MainAsync()
        {
            Env.Load(Path.Join(GetProjectDirectory(), ".env"));
            var interactor = new MongoInteractor(Environment.GetEnvironmentVariable("CONNECTION_STRING")! /* This should 100% be defined, so it's not null. */);

            // Make sure to start the client first before doing anything with the interactor!
            await interactor.StartAsync(); // Use `interactor.Start();" if running synchronously.

            // You can drop any useless databases from your cluster.
            // Use "interaction.DropDatabase();" if running synchronously
            await interactor.DropDatabaseAsync("irrelevant"); // Replace "irrelevant" with your actual database name

            // Let's get our awesome database!
            var database = interactor.GetDatabase("test"); // Make sure that "test" is replaced with your actual database name (not the cluster name)!

            // Similarly to dropping databases, we can drop a collection.
            // Make sure to use "database.DropCollection()" if running synchronously
            await database.DropCollectionAsync("irrelevant");

            // We can also create collections!
            // Again, make sure to use "database.CreateCollection()" if running synchronously
            await database.CreateCollectionAsync("helpful-data");

            // Let's get a collection, make sure to view "Models/User.cs" to see the schema.
            var collection = database.GetCollection<User>("users"); // Replace the User generic type with your actual document type and "users" with your collection name

            // All right, let's create a piece of data.
            var document = new User()
            {
                Id = 1,
                Name = "John Doe",
                Email = "johndoe@example.com",
                Password = "password"
            };

            // Use "collection.InsertOne()" if running synchronously.
            await collection.InsertOneAsync(document);

            // Let's save a filter since we're gonna use it a lot here.
            var filter = new FilterDefinitionBuilder<User>().Eq((user) => user.Id, document.Id);

            // User updated their password, let's change it!
            var password = "br4nd$%&N3w$#*P4SsWoRD!!!"; // This is just an example.

            // Make sure to use "collection.FindOneAndUpdate()" if running synchronously.
            await collection.FindOneAndUpdateAsync(filter, new UpdateDefinitionBuilder<User>().Set((user) => user.Password, password));
        }

        // Useful for Visual Studio
        private static string GetProjectDirectory()
        {
            var cwd = Directory.GetCurrentDirectory();
            string projectDirectory;

            if (cwd.Contains("bin")) // Project is running from the "bin/{Configuration}/{Framework}/{ProjectName}.exe" file
            {
                projectDirectory = Directory.GetParent(cwd)!.Parent!.Parent!.FullName;
            }
            else
            {
                projectDirectory = cwd;
            }

            return projectDirectory;
        }
    }
}

Ready to use this package? I'm sure you'll love it!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 217 11/8/2023

Initial Release
 ]