时间:2021-07-01 10:21:17 帮助过:22人阅读
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 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<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")); } } }
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; } public int Minutes { get; set; } public string Comments { get; set; } } }
C# 操作 MongoDB
标签:minutes ide nbsp root mongo ted efi public system