C #/. Net Core access to the MongoDB library

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.