Previously used Nodejs to connect the operation MongoDB, but recently need to use C # to operate MongoDB needs, so the next C # driver research. MongoDB officially provides C # driver source Https://github.com/mongodb/mongo-csharp-driver on GitHub. Source download good after compiling get MongoDB.Bson.dll and MongoDB.Driver.dll two files, I have compiled the Http://yunpan.cn/QCFn3v86KaEST access password F0ef.
Add two files to the project reference and then add the appropriate using statements to the program. Then do a simple operation on it:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingMongodb.bson;usingMongodb.driver;namespacemongodbdriverlearn{classProgram {Static voidMain (string[] args) { varClient =NewMongoclient ("Mongodb://sa:[email protected]:27017"); varServer =client. Getserver (); varDatabase = Server. Getdatabase ("Demodatabase"); varCollection = database. GetCollection ("democollection"); Collection. Insert (NewBsondocument ("Name","Jack")); foreach(varDocumentinchcollection. FindAll ()) {Console.WriteLine (document["Name"]); } console.read (); } }}