C# 操作 MongoDB的示例demo
今项目使用Mongodb,C#操作MongoDB使用MongoDB.Driver.dll库(Nuget),写了个小Demo,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using MongoDB.Driver; namespace MongoBase { internal class MongoBase { private static string _connStr = "mongodb://root:123456@127.0.0.1:27017"; private static string _dbName = "test"; private static MongoUrl url = new MongoUrl(_connStr); private static MongoClient client = new MongoClient(url); public static IMongoDatabase db = client.GetDatabase(_dbName); } public class MongoHelper<TEntity> { public string Name => typeof(TEntity).Name; public IMongoCollection<TEntity> Collection => MongoBase.db.GetCollection<TEntity>(Name); public List<TEntity> Queryable => Collection.AsQueryable().ToList(); public FilterDefinitionBuilder<TEntity> Filter => Builders<TEntity>.Filter; public UpdateDefinitionBuilder<TEntity> Update => Builders<TEntity>.Update; public ProjectionDefinitionBuilder<TEntity>Projection=> Builders<TEntity>.Projection; } }
using System; using System.Collections.Generic; using System.Text; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; namespace MongoBase { public class EntityBase { public EntityBase() { _id = ObjectId.GenerateNewId(); createTime = DateTime.Now; isDelete = false; id = _id.ToString(); } [BsonId] public ObjectId _id { get; set; } public string id { get; set; } public DateTime? createTime { get; set; } public bool? isDelete { get; set; } } }
using MongoBase; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; namespace ConsoleApp5 { internal class Video : EntityBase { public string Title { get; set; } public string Category { get; set【原URL http://www.yidunidc.com/usa.html复制请保留原URL】; } public int Minutes { get; set; } public string Comments { get; set; } } }
using System; using System.Collections.Generic; using MongoBase; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Linq; namespace ConsoleApp5 { class Program { static void Main(string[] args) { MongoHelper<Video> videoHelper = new MongoHelper<Video>(); //新增 videoHelper.Collection.InsertOne(new Video() { Title = "ggg" }); //查找集合 var list = videoHelper.Collection.Find(videoHelper.Filter.Eq(e => e.Category, "Horror")).ToList(); list = videoHelper.Collection.Find(videoHelper.Filter.Eq(e => e.Title, "ggg")).ToList(); //方式一:拼接查找条件 List<FilterDefinition<Video>> listFilter = new List<FilterDefinition<Video>>() { videoHelper.Filter.Eq(e=>e.Minutes,118), videoHelper.Filter.Eq(e=>e.Title,"The Perfect Developer") }; //根据查找多个条件筛选集合 list = videoHelper.Collection.Find(videoHelper.Filter.And(listFilter)).ToList(); //方式二:拼接查找条件 var filter = videoHelper.Filter.Eq(e => e.Title, "cys") & videoHelper.Filter.Eq(e => e.Category, "cys"); list = videoHelper.Collection.Find(filter).ToList(); //拼接更新字段 var updateDefinition = new List<UpdateDefinition<Video>>() { videoHelper.Update.Set(e=>e.Title, "cys"), videoHelper.Update.Set(e=>e.Category, "cys") }; //按照条件更新 videoHelper.Collection.UpdateMany(videoHelper.Filter.And(listFilter), videoHelper.Update.Combine(updateDefinition)); //删除 videoHelper.Collection.DeleteOne(videoHelper.Filter.Eq(e => e.Title, "Lost In Frankfurt am Main")); //获取某个字段 filter = videoHelper.Filter.Eq(e => e.Title, "ggg"); var bson = videoHelper.Collection.Find(filter).Project(videoHelper.Projection.Include(e => e.Title).Exclude(e => e._id)).FirstOrDefault(); } } }
注:时间类型的属性,存入MongoDB后会比北京时间少8小时,是由于默认写入的是世界标准时间,可在时间类型属性上添加
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime createTime { get; set; }
的特性,这样取出来之后就是北京时间。
以上就是C# 操作 MongoDB的示例demo的详细内容,更多关于C# 操作 MongoDB的资料请关注海外IDC网其它相关文章!
【本文来源:http://www.yidunidc.com/sin.html 请说明出处】