基础配置(连接、访问等)
public class MongoDBHelper<T> where T : class
{
private static MongoClient _mongoClient;
private static IMongoDatabase _mongodb;
private static IMongoCollection<T> _collection;
static MongoDBHelper()
{
_mongoClient = new MongoClient("mongodb://root:toor@127.0.0.1:27017/demo");
_mongodb = _mongoClient.GetDatabase("demo");
_collection = _mongodb.GetCollection<T>(typeof(T).CollectionName());
}
/// <summary>
/// 条件查询
/// </summary>
/// <typeparam name="T">查询对象的类型</typeparam>
/// <param name="filter">查询条件</param>
/// <returns></returns>
public static async Task<T> GetOne(FilterDefinition<T> filter)
{
return await _collection.Find<T>(filter).FirstOrDefaultAsync();
}
/// <summary>
/// 插入
/// </summary>
/// <typeparam name="T">插入对象的类型</typeparam>
/// <param name="obj">插入对象</param>
public static async Task Insert(T obj)
{
await _collection.InsertOneAsync(obj);
}
/// <summary>
/// 批量导入
/// </summary>
/// <typeparam name="T">插入对象的类型</typeparam>
/// <param name="obj">插入对象</param>
public static async Task BatchImport(List<T> objlist)
{
await _collection.InsertManyAsync(objlist);
}
/// <summary>
/// 批量插入
/// </summary>
/// <typeparam name="T">插入对象的类型</typeparam>
/// <param name="objlist">插入对象集合</param>
public static async Task Insert(List<T> objlist)
{
objlist.ForEach(async o => await _collection.InsertOneAsync(o));
}
public static async Task<UpdateResult> UpdateOne(FilterDefinition<T> filter, UpdateDefinition<T> update)
{
return await _collection.UpdateOneAsync(filter, update);
}
public static async Task<UpdateResult> UpdateMany(FilterDefinition<T> filter, UpdateDefinition<T> update)
{
return await _collection.UpdateManyAsync(filter, update);
}
public static async Task<DeleteResult> RemoveOne(FilterDefinition<T> filter)
{
return await _collection.DeleteOneAsync(filter);
}
public static async Task<DeleteResult> RemoveMany(FilterDefinition<T> filter)
{
return await _collection.DeleteManyAsync(filter);
}
public static async Task<long> GetCount(FilterDefinition<T> filter)
{
return await Task.FromResult(_collection.CountDocuments(filter));
}
}
增删改查:
class Program
{
static async Task Main(string[] args)
{
// await Person(); //带——ID
//不带ID
var ps = new List<Student>()
{
new Student() {Name = "张三", Age = 17, Loction = "北京前门", Brith = DateTime.Now.AddDays(-1000) },
new Student() {Name = "李四", Age = 17, Loction = "北京三里墩", Brith = DateTime.Now.AddDays(-6000) },
new Student() {Name = "王五", Age = 17, Loction = "北京天安门", Brith = DateTime.Now.AddDays(-5000) },
new Student() {Name = "赵六", Age = 17, Loction = "北京大栅栏", Brith = DateTime.Now.AddDays(-4000) },
new Student() {Name = "孙琦", Age = 17, Loction = "北京天桥", Brith = DateTime.Now.AddDays(-3000) }
};
//await MongoServerFactory<Student>.Insert(ps);
var builder = Builders<Student>.Filter;
var filter = builder.Eq(x=>x.Name,"张三");
var s = await MongoServerFactory<Student>.GetOne(filter);
Console.WriteLine(s);
}
/// <summary>
/// 自带ID
/// </summary>
/// <returns></returns>
static async Task Person()
{
try
{
var ps = new List<Person>()
{
new Person() {Id=1, Name = "张三", Age = 17, Loction = "北京前门", Brith = DateTime.Now.AddDays(-1000) },
new Person() {Id=2,Name = "李四", Age = 17, Loction = "北京三里墩", Brith = DateTime.Now.AddDays(-6000) },
new Person() {Id=3,Name = "王五", Age = 17, Loction = "北京天安门", Brith = DateTime.Now.AddDays(-5000) },
new Person() {Id=4,Name = "赵六", Age = 17, Loction = "北京大栅栏", Brith = DateTime.Now.AddDays(-4000) },
new Person() {Id=5,Name = "孙琦", Age = 17, Loction = "北京天桥", Brith = DateTime.Now.AddDays(-3000) }
};
// await MongoServerFactory<Person>.Insert(new Person() { Name = "李四", Age = 17, Loction = "北京三里墩", Brith = DateTime.Now.AddDays(-6000) });
await MongoServerFactory<Person>.Insert(ps);
var builder = Builders<Person>.Filter;
var filter = builder.Eq(x => x.Name, "张三");
var p = await MongoServerFactory<Person>.GetOne(filter);
Console.WriteLine(p);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Base Model:
public class Person
{
public long Id { get; set; } //自带主键
public string Name { get; set; }
public int Age { get; set; }
public string Loction { get; set; }
public DateTime Brith { get; set; }
public override string ToString()
{
return $"name:{Name},age:{Age},location:{Loction},brith:{Brith}";
}
}
[Collection("Study")]
[BsonIgnoreExtraElements] //取消系统默认的主键
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Loction { get; set; }
public DateTime Brith { get; set; }
public override string ToString()
{
return $"name:{Name},age:{Age},location:{Loction},brith:{Brith}";
}
}
敬请关注:芒果DB三大坑!
1.mongoDB生成的主键,导致数据无法取出
2.DateTime 时区不对
3.自定义ID生成规则
https://www.bilibili.com/read/cv7203263
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/267340.html