Not much to say directly on the code
Connection string:
{ "AppSettings": { "mongodb" " mongodb://user name: Password @ip Address: port number " }}
Main code:
1 usingABCDEFG. Config;2 usingMongodb.driver;3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.Linq.Expressions;6 usingSystem.Text;7 8 namespaceMongodb9 {Ten Public classMongocontext One { A PublicMongocontext () - { -Client =NewMongoclient (ABCDEFG. Getappsetting ("MongoDB")); the } - - PublicMongocontext (stringconnectionname) - { +Client =Newmongoclient (mengtconfig.getappsetting (ConnectionName)); - } + A PrivateMongoclient Client {Get;Set; } at - PrivateImongodatabase DataBase {Get= Client.getdatabase ("Mengtlog"); } - - PublicImongocollection<t> dbset<t> ()whereT:imongomodel = database.getcollection<t> ("Mengtlog.logger"); - - } in - Public Static classMongoextend to { + Public Static voidAdd<t> ( ThisImongocollection<t> collenction, T Model)whereT:imongomodel -=collenction. Insertone (Model); the * Public Static voidAddlist<t> ( ThisImongocollection<t> collenction, list<t> Model)whereT:imongomodel $=collenction. Insertmany (Model);Panax Notoginseng - /// <summary> the ///find the first one + /// </summary> A Public StaticT firstordefault<t> ( ThisImongocollection<t> Collenction,expression<func<t,BOOL>> expression)whereT:imongomodel the { + if(Expression = =NULL) {Throw NewArgumentNullException ("Invalid parameter"); } - returncollenction. Find (expression). FirstOrDefault (); $ } $ - /// <summary> - ///find a list of compliant data the /// </summary> - Public StaticList<t> findtolist<t> ( ThisImongocollection<t> collenction, Expression<func<t,BOOL>> expression)whereT:imongomodelWuyi { the if(Expression = =NULL) {Throw NewArgumentNullException ("Invalid parameter"); } - returncollenction. Find (expression). ToList (); Wu } - About /// <summary> $ ///Delete all matching data - /// </summary> - Public Static voidDelete<t> ( ThisImongocollection<t> collenction, Expression<func<t,BOOL>> expression)whereT:imongomodel - { A if(Expression = =NULL) {Throw NewArgumentNullException ("Invalid parameter"); } + collenction. Deletemanyasync (expression); the } - $ the /// <summary> the ///Delete a the /// </summary> the Public Static voidDeleteone<t> ( ThisImongocollection<t> collenction, Expression<func<t,BOOL>> expression)whereT:imongomodel - { in if(Expression = =NULL) {Throw NewArgumentNullException ("Invalid parameter"); } the collenction. Deleteoneasync (expression); the } About } the}
Imongomodel:
1 usingMongodb.bson;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Text;5 6 namespaceMongodb7 {8 Public Partial classImongomodel9 {Ten /// <summary> One ///Base ID A /// </summary> - PublicObjectId _id {Get;Set; } - } the}
It is worth noting that Mongodb.bson and mongodb.driver should be referenced.
VS2017 if the namespace cannot be found after referencing the package, restart vs.
ABCDEFG. Getappsetting ("mongodb")); Read configuration file
C #/. Net Core access to the MongoDB library