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();
        }
    }
}
相关文章
相关标签/搜索