Mongo repository.
Install-Package Aoxe.Mongo
Model
public class TestModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Initialize the client(It is recommended to store a MongoClient instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.)
AoxeMongoClient = new AoxeMongoClient(new AoxeMongoOptions
{
ConnectionString = "mongodb://admin:[email protected]:27017/admin?authSource=admin&replicaSet=rs",
Database = "TestDB"
});
Add
mongoClient.Add(model);
mongoClient.AddRange(models);
Delete
mongoClient.Delete(model);
mongoClient.Delete<TestModel>(model => names.Contains(model.Name));
Update
model.Name = "banana";
mongoClient.Update(model);
mongoClient.Update(() => new TestModel
{
Age = 22,
Name = "pear"
},
p => names.Contains(p.Name);)
Query
var query = mongoClient.GetQueryable<TestModel>();
var result = query.First(p => p.Name == "pear");
var result = query.Where(p => names.Contains(p.Name)).ToList();
Notice1:The Aoxe.Mongo will give priority to the property with BsonIdAttribute as the Primary Key.If not found,the property which named "Id"/"id"/"_id" will be follow. Notice2:TableAttribute in System.ComponentModel.Annotations can be used to mapping the document name.