This project uses the mongodb,c# operation MongoDB to use the MongoDB.Driver.dll Library (Nuget), wrote a small demo, as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;usingSystem.Text;usingMongodb.driver;namespacemongobase{Internal classMongobase {Private Static string_connstr ="Mongodb://root:[email protected]:27017"; Private Static string_dbname ="Test"; Private StaticMongourl URL =NewMongourl (_CONNSTR); Private StaticMongoclient client =Newmongoclient (URL); Public StaticImongodatabase db =client. Getdatabase (_dbname); } Public classMongohelper<tentity> { Public stringName =typeof(TEntity). Name; Publicimongocollection<tentity> Collection = mongobase.db.getcollection<tentity>(Name); Publiclist<tentity> queryable =collection.asqueryable (). ToList (); Publicfilterdefinitionbuilder<tentity> Filter = builders<tentity>. Filter; Publicupdatedefinitionbuilder<tentity> Update = builders<tentity>. Update; }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingMongodb.bson;usingMongoDB.Bson.Serialization.Attributes;namespacemongobase{ Public classEntityBase { Publicentitybase () {_id=Objectid.generatenewid (); Createtime=DateTime.Now; Isdelete=false; ID=_id. ToString (); } [Bsonid] PublicObjectId _id {Get;Set; } Public stringID {Get;Set; } PublicDatetime? Createtime {Get;Set; } Public BOOL? Isdelete {Get;Set; } }}
usingSystem;usingSystem.Collections.Generic;usingmongobase;usingMongodb.bson;usingMongodb.driver;usingMongoDB.Driver.Linq;namespaceconsoleapp5{classProgram {Static voidMain (string[] args) {Mongohelper<Video> Videohelper =NewMongohelper<video>(); //NewVideoHelper.Collection.InsertOne (NewVideo () {Title="GGG" }); //Find Collection varList = VideoHelper.Collection.Find (VideoHelper.Filter.Eq (e = e.category,"Horror")). ToList (); //Way One: stitching find conditionsList<filterdefinition<video>> Listfilter =NewList<filterdefinition<video>>() {videoHelper.Filter.Eq (e=>e.minutes,118), VideoHelper.Filter.Eq (e=>e.title,"The Perfect Developer") }; //Filter collections by finding multiple criteriaList =VideoHelper.Collection.Find (VideoHelper.Filter.And (Listfilter)). ToList (); //mode two: stitching search conditions varFilter = VideoHelper.Filter.Eq (E = E.title,"Cys") & VideoHelper.Filter.Eq (e = e.category,"Cys"); List=VideoHelper.Collection.Find (filter). ToList (); //Stitching Update Fields varUpdatedefinition =NewList<updatedefinition<video>>() {VideoHelper.Update.Set (e=>e.title,"Cys"), VideoHelper.Update.Set (e=>e.category,"Cys") }; //Update by ConditionVideoHelper.Collection.UpdateMany (VideoHelper.Filter.And (listfilter), videohelper.update.c Ombine (updatedefinition)); //DeleteVideoHelper.Collection.DeleteOne (videoHelper.Filter.Eq (e = E.title,"Lost in Frankfurt am Main")); } }}
usingmongobase;usingMongodb.bson;usingMongoDB.Bson.Serialization.Attributes;namespaceconsoleapp5{Internal classVideo:entitybase { Public stringTitle {Get;Set; } Public stringCategory {Get;Set; } Public intMinutes {Get;Set; } Public stringComments {Get;Set; } }}
C # Operation MongoDB