MongoDB.Thin
1.0.0
dotnet add package MongoDB.Thin --version 1.0.0
NuGet\Install-Package MongoDB.Thin -Version 1.0.0
<PackageReference Include="MongoDB.Thin" Version="1.0.0" />
paket add MongoDB.Thin --version 1.0.0
#r "nuget: MongoDB.Thin, 1.0.0"
// Install MongoDB.Thin as a Cake Addin #addin nuget:?package=MongoDB.Thin&version=1.0.0 // Install MongoDB.Thin as a Cake Tool #tool nuget:?package=MongoDB.Thin&version=1.0.0
MongoDB.Thin
Thin layer over MongoDB driver for .NET. It is mostly extension methods and builders. It allows to query MongoDB more easily. The goal of this library is to stay simple, more complex cases can be built upon.
Nuget
The nuget package name is MongoDB.Thin
: https://www.nuget.org/packages/MongoDB.Thin/
Usage
ASP.NET Core
To add it to ASP.NET Core
services.AddMongo("YourConnectionString", "YourDatabaseName");
Get a collection
Example where _database is an instance of IMongoDatabase. And Game is the collection holding the games.
_database.Collection<Game>();
FindOne
Get a document from a collection by supplying a filtering c# expression Example:
var game = await _games.FindOneAsync(g => g.Id == id);
FindOneAndUpdate
Get a FindOneAndUpdate command by invoking FindOneAndUpdate()
on your collection (IMongoCollection<TDocument>).
You can then filter it multiple times, and perform multiple modifications.
- Filters are defined by calling
Filter()
on the command, the method accepts a lambda that configures the filter builder: C# filtering expression, C# expression with MongoDB operators, or a JSON string. - Modifications are defined by calling
Modify()
on the command, the method accepts a lambda that configures the modification builder. Normal MongoDB operators can be used. - Options can be defined by calling
Options()
on the command, the method accepts a lambda that configures the options builder. Projections can be defined, targeting array fields can be defined usingWithArrayFilter
. - Finally awaiting
ExecuteAsync
runs it asynchronously.
Example:
var command = _games.FindOneAndUpdate();
command.Filter(b => b.Match(g => g.GameType == gameType &&
!g.Players.Any(p => p.Id == player.Id) &&
g.Status == GameStatus.WaitingForPlayers));
if (duration.HasValue)
{
command = command.Filter(b => b.Match(g => g.MaxDuration <= duration));
}
var game = await command.Update(b => b
.Modify(g => g.Push(g => g.Players, player))
.Modify(g => g.Inc(g => g.Version, 1)))
.ExecuteAsync();
UpdateOne
Get an UpdateOne command by invoking UpdateOne()
on your collection (IMongoCollection<TDocument>).
The command is similar to FindOneAndUpdate, except that the options don't allow any projection (since no document is returned).
Indexes
Indexes can easily be added using expressions.
Example:
var db = app.ApplicationServices.GetService<IMongoDatabase>()!;
db.Collection<Game>().Indexes
.Add(g => g.GameType)
.Add(g => g.Status)
.Add(g => g.Players, p => p.Id);
Here Game collection got 3 indexes:
- GameType field ascending
- Status field ascending
- Players is an array, it got an index on Id field
More usage examples
You can see more usage examples in OnlineBoardz repository:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.6)
- MongoDB.Driver (>= 2.10.4)
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 | 2,134 | 7/22/2020 |