mongodb使用c#驅動數據插入demo

今天小編就爲大家分享一篇關於mongodb使用c#驅動數據插入demo,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

Mongodb提供了多種開發語言的驅動,java,python,c++,c# 等,這裏選用c#驅動作爲測試;

首先上mongo官網下載驅動。Ps:官方網站經常連接不順利。

還不如直接在vs的nuget管理包中搜索mongoDB.driver.

需要引入的命名空間:

using MongoDB.Bson;
using MongoDB.Driver;

Driver是驅動核心,Bson是和數據格式相關的;

定義一個mongo客戶端,一個mongodb,一個數據集合;

protected staticIMongoClient client;
protected staticIMongoDatabase database;
protected staticIMongoCollection<BsonDocument> collection;

連接上MongoDB

//定義連接
client = new MongoClient("mongodb://127.0.0.1:27017");
//獲取test數據庫
database = client.GetDatabase("test");     
//獲取test數據庫中的集合bios
collection = database.GetCollection<BsonDocument>("bios");

這裏解釋說明下:首先你得讓mongod(mongo的服務端)運行起來,不然服務端都沒開,怎麼連接呢;目前測試還沒有涉及到安全以及用戶權限數據庫管理這塊,所以這裏的連接都是使用的默認不帶用戶登錄驗證;

需求注意的是,如果我們建立的是控制檯程序,那麼這個連接必須寫地址必須帶端口,就像上面所寫;

如果是建立的一個MVC web,你僅僅是測試數據插入,在這種無安全驗證的方式下,你可以省去連接字符串。

如下圖;

接下來就是定義一個測試數據:

var document =new BsonDocument
      {
          { "address" , newBsonDocument
            {
              { "street","2 Avenue" },
              { "zipcode","10075" },
              { "building","1480" },
              { "coord",new BsonArray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "Manhattan"},
          { "cuisine", "Italian"},
          { "grades", new BsonArray
              {
                new BsonDocument
                {
                  { "date",new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade","A" },
                  { "score",11 }
                },
                new BsonDocument
                {
                  { "date",new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade","B" },
                  { "score",17 }
                }
              }
          },
          { "name", "Vella"},
          { "restaurant_id","41704620" }
      };

最後調用InsertOneAsync()方法;

collection.InsertOneAsync(document);

最終插入結果:

這裏使用shell來看數據的話就太不直觀了,這裏使用的是比較常用的一個mongodb可視化管理工具Robomongo 

附上代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace mongodbInsert
{
  class Program
  {
    protected static IMongoClient client;
    protected static IMongoDatabase database;
    protected static IMongoCollection<BsonDocument> collection; 
    static void Main(string[] args)
    {
       client = new MongoClient("mongodb://127.0.0.1:27017");
       database = client.GetDatabase("test");
       collection = database.GetCollection<BsonDocument>("bios");
       for (int i = 0; i < 14; i++)
       {
         var document = new BsonDocument
      {
          { "address" , new BsonDocument
            {
              { "street", "2 Avenue" },
              { "zipcode", "10075" },
              { "building", "1480" },
              { "coord", new BsonArray { 73.9557413, 40.7720266 } }
            }
          },
          { "borough", "Manhattan" },
          { "cuisine", "Italian" },
          { "grades", new BsonArray
              {
                new BsonDocument
                {
                  { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade", "A" },
                  { "score", 11 }
                },
                new BsonDocument
                {
                  { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                  { "grade", "B" },
                  { "score", 17 }
                }
              }
          },
          { "name", "Vella" },
          { "restaurant_id", "41704620" }
      };
         collection.InsertOneAsync(document);
       }
       Console.WriteLine();
       Console.ReadLine();
    }
  }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章