C# MongoDB基本使用
基础配置(连接、访问等)
public class MongoDBHelperwhere T : class { private static MongoClient _mongoClient; private static IMongoDatabase _mongodb; private static IMongoCollection _collection; static MongoDBHelper() { _mongoClient = new MongoClient("mongodb://root:toor@127.0.0.1:27017/demo"); _mongodb = _mongoClient.GetDatabase("demo"); _collection = _mongodb.GetCollection (typeof(T).CollectionName()); } /// /// 条件查询 /// /// 查询对象的类型 /// 查询条件 /// public static async Task GetOne(FilterDefinition filter) { return await _collection.Find (filter).FirstOrDefaultAsync(); } /// /// 插入 /// /// 插入对象的类型 /// 插入对象 public static async Task Insert(T obj) { await _collection.InsertOneAsync(obj); } /// /// 批量导入 /// /// 插入对象的类型 /// 插入对象 public static async Task BatchImport(List objlist) { await _collection.InsertManyAsync(objlist); } /// /// 批量插入 /// /// 插入对象的类型 /// 插入对象集合 public static async Task Insert(List objlist) { objlist.ForEach(async o => await _collection.InsertOneAsync(o)); } public static async Task UpdateOne(FilterDefinition filter, UpdateDefinition update) { return await _collection.UpdateOneAsync(filter, update); } public static async Task UpdateMany(FilterDefinition filter, UpdateDefinition update) { return await _collection.UpdateManyAsync(filter, update); } public static async Task RemoveOne(FilterDefinition filter) { return await _collection.DeleteOneAsync(filter); } public static async Task RemoveMany(FilterDefinition filter) { return await _collection.DeleteManyAsync(filter); } public static async Task<long> GetCount(FilterDefinition filter) { return await Task.FromResult(_collection.CountDocuments(filter)); } }
增删改查:
class Program { static async Task Main(string[] args) { // await Person(); //带——ID //不带ID var ps = new List() { 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 .Insert(ps); var builder = Builders.Filter; var filter = builder.Eq(x=>x.Name,"张三"); var s = await MongoServerFactory .GetOne(filter); Console.WriteLine(s); } /// /// 自带ID /// /// static async Task Person() { try { var ps = new List () { 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 .Insert(new Person() { Name = "李四", Age = 17, Loction = "北京三里墩", Brith = DateTime.Now.AddDays(-6000) }); await MongoServerFactory.Insert(ps); var builder = Builders .Filter; var filter = builder.Eq(x => x.Name, "张三"); var p = await MongoServerFactory .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