mongoDB之C#and.net Driver

  以前一直都是用NodeJS來鏈接操做mongoDB的,可是最近須要用C#操做mongoDB的須要,因此研究了下C#驅動。mongoDB官方在GitHub上提供了C#驅動源碼https://github.com/mongodb/mongo-csharp-driver。源碼下載好以後編譯獲得MongoDB.Bson.dll和MongoDB.Driver.dll兩個文件,我已經編譯好了http://yunpan.cn/QCFn3v86KaEST  訪問密碼 f0ef。git

  將兩個文件添加到項目引用而後在程序中添加相應的using語句。而後對其進行簡單的操做:github

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MongoDB.Bson;
using MongoDB.Driver;

namespace MongoDBDriverLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new MongoClient("mongodb://sa:sa@localhost:27017");
            var server = client.GetServer();
            var database = server.GetDatabase("demoDatabase");
            var collection = database.GetCollection("demoCollection");

            collection.Insert(new BsonDocument("Name", "Jack"));

            foreach (var document in collection.FindAll())
            {
                Console.WriteLine(document["Name"]);
            }

            Console.Read();
        }
    }
}
相關文章
相關標籤/搜索