MongoGogo 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MongoGogo --version 2.0.0                
NuGet\Install-Package MongoGogo -Version 2.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="MongoGogo" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoGogo --version 2.0.0                
#r "nuget: MongoGogo, 2.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 MongoGogo as a Cake Addin
#addin nuget:?package=MongoGogo&version=2.0.0

// Install MongoGogo as a Cake Tool
#tool nuget:?package=MongoGogo&version=2.0.0                

MongoGogo Guide

Install via .NET CLI

dotnet add package MongoGogo 

Example

  • After few steps of configuration and class building(introduced later), you can easily get your Repository in abstract through the dependency resolution system.
public class MyController : ControllerBase
{
    private readonly IGoRepository<Hospital> HospitalRepository;

    public MyController(IGoRepository<Hospital> hospitalRepository)
    {
    	this.HospitalRepository = hospitalRepository;
    }
}
  • Easy way to access data.
public class MyController : ControllerBase
{
    [HttpGet("IGoRepository<Hospital>_GetList")]
    public async Task<IActionResult> Hospitals()
    {
        var hospitals = await HospitalRepository.FindAsync(_ => true);
        return Ok(hospitals);
    }
}
  • Also, MongoGogo provides any Collection, Database and context in abstract !
public class MyController : ControllerBase
{
    private readonly IGoContext<MyMongoDBContext> MyContext;
    private readonly IGoDatabase<MyMongoDBContext.City> CityDatabase;
    private readonly IGoCollection<Hospital> HospitalCollection;

    public MyController(IGoContext<MyMongoDBContext> myContext,
                        IGoDatabase<MyMongoDBContext.City> cityDatabase,
                        IGoCollection<Hospital> hospitalCollection)
    {
        this.MyContext = myContext;
        this.CityDatabase = cityDatabase;
        this.HospitalCollection = hospitalCollection;
    }
}

IGoContext<T>IGoDatabase<T> and IGoCollection<T>:

  • is in generic type with very few restriction
  • acts like IMongoClientIMongoDatabase and IMongoCollection<T> of the well-known package MongoDB.Driver.
using MongoDB.Driver;
public class MyController : ControllerBase
{
    [HttpGet("IGoCollection<T>_GetList")]
    public async Task<IActionResult> Collection()
    {
        var hospitals = (await HospitalCollection.FindAsync(_ => true)).ToEnumerable();
        return Ok(hospitals);
    }

    [HttpGet("IGoDatabase<T>_ListAllCollections")]
    public IActionResult Database()
    {
        var collectionNames = CityDatabase.ListCollectionNames().ToList();
        return Ok(collectionNames);
    }

    [HttpGet("IGoContext_DeleteDatabasebyName")]
    public IActionResult Context(string databaseName)
    {
        MyContext.DropDatabase(databaseName);
        return Ok($"{databaseName} is successfully droped.");
    }
}

Configuration

In the ConfigureSerivces segment of .net core, pass an IGoContext instance into your IServiceCollection.

The build of concrete class MyMongoDBContext is introduced next part.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMongoContext(new MyMongoDBContext("my mongodb connection string"));
}

Or, you can configure mongoContext through implementationFactory

public void ConfigureServices(IServiceCollection services)
{
    Func<IServiceProvider, object> implementationFactory; //some custom factory
    services.AddMongoContext<MyMongoDBContext>(implementationFactory);
}

  • LifeCycleOption: Singleton, Scoped, Transient

Besides, you can controller the lifecyle of all IGoContext<T>, IGoDatabase<T>, IGoCollection<T>, IGoRepository<T> using LifeCycleOption. Scoped by default.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMongoContext(new MyMongoDBContext("my mongodb connection string"),
    						 new LifeCycleOption
                                 {
                                     ContextLifeCycle = LifeCycleType.Singleton,
                                     DatabaseLifeCycle = LifeCycleType.Scoped,
                                     CollectionLifeCycle = LifeCycleType.Scoped,
                                     RepositoryLifeCycle = LifeCycleType.Transient,
                                 });
}

Context

IGoContext<TContext>

  • Stands for a MongoDB Connection (using package MongoDB.Driver)
  • an instance stores connection string and is in charge of the structure of an entity.
  • generic TContext, which owns the type itself, is used for dependency resolution system.
public class MyMongoDBContext : GoContext<MyMongoDBContext>
{
    [MongoDatabase]
    public class City { }

    [MongoDatabase("Log")]
    public class Logger { }

    [MongoDatabase]
    public class Admin { }
    
    public MyMongoDBContext(string connectionString) : base(connectionString)
    {
    }
}
public abstract class GoContext<TContext> : IGoContext<TContext> 
{
    //...
}

Congratulations!! You successful set up the database in the IGoContext.

A vaild database of an entity/context must be:

  • an inner class of IGoContext<TContext> which is public or internal
  • decorated by MongoDatabase attribute

Collections

Provided the database City has two collection City and Hospital.

  • decorated by MongoCollection attribute, and specify corresponding database type.
  • Notice once again, this database type must be an inner class of an IGoContext<TContext>
[MongoCollection(fromDatabase: typeof(MyMongoDBContext.City))]
public class City
{
    [BsonId]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

    public int Polulation { get; set; }
}

Besides, keep the old way using attribute of MongoDB.Driver, if you have some restriction on this collection, field, or even some Serializer.

using MongoDB.Bson.Serialization.Attributes;

[MongoCollection(typeof(MyMongoDBContext.City),"Hospital"]
[BsonIgnoreExtraElements]
public class Hospital
{
    [BsonId]
    public ObjectId _id { get; set; }

    [BsonElement("FullName")]
    public string Name { get; set; }

    [BsonIgnoreIfDefault]
    public City City { get; set; }
}

Repositories

By default, resolution system will build exactly one IGoRepository<TDocument> for every IGoCollection<TDocument> .

IGoRepository<TDocument> is in charge of the contact of a MongoDb connection and your project.

public class MyClass
{
	private readonly IGoRepository<Hospital> HospitalRepository;
    private readonly IGoCollection<Hospital> HospitalCollection;

    public MyClass(IGoRepository<Hospital> hospitalRepository,
    			   IGoCollection<Hospital> hospitalCollection)
    {
        this.HospitalRepository = hospitalRepository;
        this.HospitalCollection = hospitalCollection;
    }
}

In fact, You can deal with data using IGoRepository<Hospital> or IGoCollection<Hospital>.

The main difference between them is :

  • IGoCollection<Hospital> is actually the IMongoCollection<Hospital>, the instance from MongoDB.Driver
  • IGoRepository<Hospital> is an packaged instance, but with handy method to operate your datas.
public async Task AddNewHospital(Hospital hospital)
{
    HospitalRepository.InsertOne(hospital);
    HospitalCollection.InsertOne(hospital);
}
  • IGoRepository<Tdocument> has basic method dealing your data, like FindOneAsync, InsertOne, ReplaceOne, Count, and so on.
  • But if you want some extra fancy feature, you can also do it yourself by overriding the origin method !!!
Custom Repository

If the basic functionality of IGoRepository<TDocument> cannot fullfill you,

(like: print some message and saves it as a local log)

then inherit the GoRepositoryAbstract<TDocument> and make your own child.

public class HospitalRepository : GoRepositoryAbstract<Hospital>
{
    public HospitalRepository(IGoCollection<Hospital> collection) : base(collection)
    {
    }
    
    public override void InsertOne(Hospital hospital)
    {
    	base.InsertOne(hospital);
        Logger.PrintAndSave(hospital); //print insert success and save
    }
}

After this , the resolution system will map new child HospitalRepository to IGoRepository <Hospital>

public class MyClass
{
	private readonly IGoRepository<Hospital> HospitalRepository; //actually your new class HospitalRepository
    private readonly IGoCollection<Hospital> HospitalCollection; //not changed

    public MyClass(IGoRepository<Hospital> hospitalRepository,
    			   IGoCollection<Hospital> hospitalCollection)
    {
        this.HospitalRepository = hospitalRepository; //actually your new class HospitalRepository
        this.HospitalCollection = hospitalCollection; //not changed
    }
    
    public async Task AddNewHospital(Hospital hospital)
    {
        HospitalRepository.InsertOne(hospital);  // it will print insert success and save

        HospitalCollection.InsertOne(hospital);  // nothing else
    }
}

Feature

This part shows basic method of anIGoRepository<TDocument>

IGoRepository<City> CityRepository;

Count / CountAsync

var documentCount = CityRepository.Count(city => city.Population >= 1000);
var documentCountAsync = await CityRepository.CountAsync(city => city.Population >= 1000);

Find/FindAsync

var foundDocuments = CityRepository.Find(city => city.Population >= 1000);
var foundDocumentAsync = await CityRepository.FindAsync(city => city.Population >= 1000);
  • with goFindOption (optional)
var foundDocuments = CityRepository.Find(city => city.Population >= 1000,
                                         goFindOption: new GoFindOption
                                        {
                                            AllowDiskUse = true,
                                            Limit = 2,
                                            Skip = 1
                                        });
var foundDocumentAsync = await CityRepository.FindAsync(city => city.Population >= 1000, 
                                                        goFindOption: new GoFindOption
                                                        {
                                                            AllowDiskUse = true,
                                                            Limit = 2,
                                                            Skip = 1
                                                        });
  • field reduction with projection(optional)
var reducedDocuments = CityRepository.Find(city => city.Population >= 1000,
                                           projection: builder => builder.Include(city => city.Population)
                                                                         .Include(city => city.Name));
var reducedDocumentAsync = await CityRepository.FindAsync(city => city.Population >= 1000, 
                                                          projection: builder => builder.Include(city => city.Population)
                                                                                        .Include(city => city.Name));

FindOne/FindOneAsync

var firstOrDefaultDocument = CityRepository.FindOne(city => city.Population >= 1000);
var firstOrDefaultDocumentAsync = await CityRepository.FindOneAsync(city => city.Population >= 1000);

InsertOne/InsertOneAsync

CityRepository.InsertOne(new City{Name = "New York"});
await CityRepository.InsertOneAsync(new City{Name = "New York"});

InsertMany/InsertManyAsync

var cityList = new List<City>
{
	new City{}, 
	new City{Name = "New York"}, 
	new City{Population = 100}
};

CityRepository.InsertMany(cityList);
await CityRepository.InsertManyAsync(cityList);

ReplaceOne/ReplaceOneAsync

var city = new City
{
	Name = "NewYork_America"
};

CityRepository.ReplaceOne(c => c.Name == "NewYork",
						  city);
await CityRepository.ReplaceOneAsync(c => c.Name == "NewYork",
									 city);
  • with upsert(optional)
CityRepository.ReplaceOne(c => c.Name == "NewYork",
                          city,
                          isUpsert: true);
await CityRepository.ReplaceOneAsync(c => c.Name == "NewYork",
                                     city,
                                     isUpsert: true);

UpdateOne/UpdateOneAsync

CityRepository.UpdateOne(city => city.Name == "New York",
                         builder => builder.Set(city => city.Name, "New York_America")
                       					   .Set(city => city.Population, 1000));
await CityRepository.UpdateOneAsync(city => city.Name == "New York",
                                    builder => builder.Set(city => city.Name, "New York_America")
                                    				  .Set(city => city.Population, 1000));
  • with upsert(optional)
CityRepository.UpdateOne(city => city.Name == "New York",
                         builder => builder.Set(city => city.Name, "New York_America")
                       					   .Set(city => city.Population, 1000),
                         isUpsert: true);
await CityRepository.UpdateOneAsync(city => city.Name == "New York",
                                    builder => builder.Set(city => city.Name, "New York_America")
                                    				  .Set(city => city.Population, 1000),
                                   isUpsert: true);

UpdateMany/UpdateManyAsync

CityRepository.UpdateMany(city => city.Name == "New York",
                          builder => builder.Set(city => city.Name, "New York_America")
                      					    .Set(city => city.Population, 1000));
await CityRepository.UpdateManyAsync(city => city.Name == "New York",
                                     builder => builder.Set(city => city.Name, "New York_America")
 				                                       .Set(city => city.Population, 1000));

DeleteOne/DeleteOneAsync

CityRepository.DeleteOne(city => city.Name == "New York");
await CityRepository.DeleteOneAsync(city => city.Name == "New York");

DeleteMany/DeleteManyAsync

CityRepository.DeleteMany(city => city.Name == "New York");
await CityRepository.DeleteManyAsync(city => city.Name == "New York");
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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
6.0.0 66 5/20/2024
5.4.1 104 2/5/2024
5.4.0 82 2/2/2024
5.3.0 802 11/16/2023
5.2.2 120 11/3/2023
5.2.1 780 8/7/2023
5.2.0 318 7/24/2023
5.1.2 149 7/21/2023
5.1.1 239 7/4/2023
5.0.1 226 6/28/2023
4.0.0 163 6/19/2023
3.1.0 635 3/17/2023
2.0.1 551 11/18/2022
2.0.0 347 11/18/2022
1.0.3 327 11/10/2022
1.0.2 353 11/9/2022
1.0.1 328 11/9/2022
1.0.0 355 11/9/2022