SquirrelFramework.MongoDB
2.0.0
dotnet add package SquirrelFramework.MongoDB --version 2.0.0
NuGet\Install-Package SquirrelFramework.MongoDB -Version 2.0.0
<PackageReference Include="SquirrelFramework.MongoDB" Version="2.0.0" />
paket add SquirrelFramework.MongoDB --version 2.0.0
#r "nuget: SquirrelFramework.MongoDB, 2.0.0"
// Install SquirrelFramework.MongoDB as a Cake Addin #addin nuget:?package=SquirrelFramework.MongoDB&version=2.0.0 // Install SquirrelFramework.MongoDB as a Cake Tool #tool nuget:?package=SquirrelFramework.MongoDB&version=2.0.0
SquirrelFramework.MongoDB 2.0.0
Squirrel Framework - A lightweight back-end framework and utilities kit based on MongoDB.
The word Squirrel has the meaning of squirrels and storage. The Squirrel Framework is working to make your MongoDB / Azure Cosmos DB-based applications lightweight and fast.
Release notes: 2.0.0 (Chinese, 中文)
Release notes: 1.0.15 (Chinese, 中文)
Release notes: 1.0.14 (Chinese, 中文)
Get Package
You can get the latest stable release from the official Nuget.org feed
https://www.nuget.org/packages/SquirrelFramework.MongoDB
Getting Started
Create a .NET project, please ensure that the target framework
MUST be .NET 6 or later
Get the Nuget package by searching the keyword "SquirrelFramework.MongoDB" or using the Project Manager
Install-Package SquirrelFramework.MongoDB -Version 2.0.0
Create your Domain Model
using SquirrelFramework.Domain.Model; [Database("YourDatabaseName")] [Collection("UsersCollectionName")] public class User : DomainModel { public string Name { get; set; } public string Gender { get; set; } public int Age { get; set; } }
The [Database] attribute is not necessary
, you can set the default MongoDB database name when initialing the configurations.Configurations.Configure("mongodb://localhost:27017", "Test"); MongoDBBasicTestCrudRepository = new MongoDBBasicTestCrudRepository(); var pingResult = MongoDBBasicTestCrudRepository.Ping(); Console.WriteLine(pingResult);
Since 1.0.14 the [Collection] attribute is no longer required.
If you not specified the [Collection] attribute, the Squirrel Framework use the class name as the collection name.Create your Repository for MongoDB CRUD
using SquirrelFramework.Repository; public class UserRepository: RepositoryBase<User> {}
Now you are free to perform various operations on MongoDB, here are some examples
var userRepo = new UserRepository();
Add a new user record
userRepo.Add(new User{ Name = "Hendry", Gender = "Male", Age = 18, Geolocation = new Geolocation(121.551949, 38.890957) });
Get all users who are 2 kilometers away from me
userRepo.GetNearBy(new Geolocation(121.551949, 38.890957), 2000);
Bulk delete users who are older than 25 asynchronously
userRepo.DeleteManyAsync(u => u.Age > 25);
Get the third page (15 records) of all user data and descending sort by the Age field
// Method signature // public IEnumerable<TDomain> GetAllByPageSortBy( // int pageIndex, // int pageSize, // Expression<Func<TDomain, object>> sortBy, // bool isSortByDescending = false // ); userRepo.GetAllByPageSortBy(2, 15, u => u.Age, true);
If your data collection is dynamic, for example you have multiple collections to store your user information:
- Users201801
- Users201802
- Users201803
- ...
You can use the CustomizedRepositoryBase class as a base class
public class UserRepository: CustomizedRepositoryBase<User> {}
Then you can provide the specific collection name for each CRUD operation.
- Add a new user record
var userRepo = new UserRepository(); userRepo.Add("Users201805", new User{ Name = "George", Gender = "Male", Age = 18, Geolocation = new Geolocation(121.551949, 38.890957) });
Create your Domain Service (This is not a necessary step)
public class UserService : ServiceBase<User, UserRepository> {}
Contributing
- George Qi me@nap7.com
If you have any questions, please feel free to contact me
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net7.0
- MongoDB.Bson (>= 2.19.2)
- MongoDB.Driver (>= 2.19.2)
- MongoDB.Driver.Core (>= 2.19.2)
- SquirrelFramework.Model (>= 2.0.5)
- SquirrelFramework.Utility (>= 2.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.