项目作者: Mutuduxf

项目描述 :
Mongo repository.
高级语言: C#
项目地址: git://github.com/Mutuduxf/Zaabee.Mongo.git
创建时间: 2018-01-27T09:48:12Z
项目社区:https://github.com/Mutuduxf/Zaabee.Mongo

开源协议:MIT License

下载


Aoxe.Mongo

English | 简体中文

Provide an easy way to use Mongo as repository and with UpdateMany / DeleteMany in lambda style.

This project has implemented three packages:

  • Aoxe.Mongo.Abstractions - The abstractions of aoxe mongo.
  • Aoxe.Mongo.Client - The implements of Aoxe.Mongo.Abstractions.
  • Aoxe.Mongo - The service provider extensions for Aoxe.Mongo.Client to simplify the IOC register.

QuickStart

Install the package from NuGet

  1. PM> Install-Package Aoxe.Mongo

Register the AoxeMongoClient into IOC

  1. serviceCollection.AddAoxeMongo("mongodb://localhost:27017", "test");

Inject the IAoxeMongoClient by reference Aoxe.Email.Abstractions.

  1. PM> Install-Package Aoxe.Mongo.Abstractions
  1. public class TestRepository
  2. {
  3. private readonly IAoxeMongoClient _mongoClient;
  4. public TestRepository(IAoxeMongoClient mongoClient)
  5. {
  6. _mongoClient = mongoClient;
  7. }
  8. }

Also you can inject the Lazy.

  1. public class TestRepository
  2. {
  3. private readonly Lazy<IAoxeMongoClient> _mongoClient;
  4. public TestRepository(Lazy<IAoxeMongoClient> mongoClient)
  5. {
  6. _mongoClient = mongoClient;
  7. }
  8. }
  1. If you don't use IOC, you can instantiate AoxeMongoClient directly.
  2. ```bash
  3. PM> Install-Package Aoxe.Mongo.Client
  1. var mongoClient = new AoxeMongoClient("mongodb://localhost:27017", "test");

Now we have a Model class like this:

  1. public class TestModel
  2. {
  3. public Guid Id { get; set; }
  4. public string Name { get; set; }
  5. public int Age { get; set; }
  6. }

And then we can use the mongo like a repository:

Add

  1. mongoClient.Add(model);
  2. mongoClient.AddRange(models);
  3. await mongoClient.AddAsync(model);
  4. await mongoClient.AddRangeAsync(models);

Delete

  1. mongoClient.Delete(model);
  2. mongoClient.DeleteMany<TestModel>(m => m.Id == model.Id);
  3. await mongoClient.DeleteAsync(model);
  4. await mongoClient.DeleteManyAsync<TestModel>(m => m.Id == model.Id);

Update

  1. model.Name = "banana";
  2. mongoClient.Update(model);
  3. mongoClient.UpdateMany(() =>
  4. m => m.Id == model.Id,
  5. new TestModel
  6. {
  7. Age = 22,
  8. Name = "pear"
  9. })
  10. await mongoClient.UpdateAsync(model);
  11. await mongoClient.UpdateManyAsync(() =>
  12. m => m.Id == model.Id,
  13. new TestModel
  14. {
  15. Age = 22,
  16. Name = "pear"
  17. })

Query

  1. var query = mongoClient.GetQueryable<TestModel>();
  2. var result = query.FirstOrDefault(p => p.Name == "pear");
  3. var result = query.Where(p => names.Contains(p.Name)).ToList();