文章目錄
C# 搭配 MongoDB 的連線寫法
最近有個新專案需要儲存 json 格式的資料,MongoDB 是考慮的選項之一,評估的過程中才發現我沒有 C# 連線 MongoDB 的使用筆記,雖然專案時程非常急迫需求假日額外加班處理,原本很認份地要開工但仔細想想工作畢竟是工作就算再急也不該為了工作失去熱誠,雖說我自己常常額外加班不過都是出於對程式熱誠,一旦只是為了對專案有所交待,我愈來愈說服不了自己,所以決定在休假期間放下時程的考量做自己覺得開心的事
立馬就來紀錄一下 MongoDB 的連線寫法吧
安裝套件 MongoDB.Driver
Install-Package MongoDB.Driver
MongoDB 的連線寫法
使用預設連線 (localhost:27017)
MongoClient client = new MongoClient();
使用連線字串
MongoClient client = new MongoClient("mongodb://localhost:27017");
使用 MongoClientSettings
MongoClient client = new MongoClient( new MongoClientSettings { Server = new MongoServerAddress("localhost",27017) });
使用 MongoUrl
MongoClient client = new MongoClient( MongoUrl.Create("mongodb://localhost:27017"));
有密碼保護的 MongoDB 連線寫法
使用預設連線 (localhost:27017)
預設連線方式無法指定帳號密碼
使用連線字串
語法
MongoClient client= new MongoClient("mongodb://{username}:{password}@{host}:{port}/{Database}");
實際範例
MongoClient client= new MongoClient("mongodb://yowkoTest:[email protected]:27017/test");
使用 MongoClientSettings
寫法一
語法
var credential = MongoCredential.CreateCredential("{Database}", "{username}", "{password}"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = credential, Server = new MongoServerAddress("{host}", {port}), });
實際範例
var credential = MongoCredential.CreateCredential("test", "yowkoTest", "passwordTest"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = credential, Server = new MongoServerAddress("localhost", 27017), });
寫法二
語法
MongoIdentity identity = new MongoInternalIdentity("{Database}", "{username}"); MongoIdentityEvidence evidence = new PasswordEvidence("{password}"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = new MongoCredential(null, identity, evidence), Server = new MongoServerAddress("{host}", {port}), });
實際範例
MongoIdentity identity = new MongoInternalIdentity("test", "yowkoTest"); MongoIdentityEvidence evidence = new PasswordEvidence("passwordTest"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = new MongoCredential(null, identity, evidence), Server = new MongoServerAddress("localhost", 27017), });
使用 MongoUrl
語法
MongoClient client = new MongoClient( MongoUrl.Create("mongodb://{username}:{password}@{host}:{port}/{Database}"));
實際範例
MongoClient client = new MongoClient( MongoUrl.Create("mongodb://yowkoTest:passwordTest@localhost:27017/test"));
有密碼保護的 MongoDB ReplicaSet 連線寫法
使用預設連線 (localhost:27017)
預設連線方式無法指定帳號密碼及 ReplicaSet
使用連線字串
語法
MongoClient client= new MongoClient("mongodb://{username}:{password}@{host1}:{port1},{host2}:{port2},{host3}:{port3}/?replicaSet={replicaSet Name}");
實際範例
MongoClient client= new MongoClient("mongodb://yowkoTest:[email protected]:27017,127.0.0.1:27027,127.0.0.1:27037/?replicaSet=TestReplicaSet");
使用 MongoClientSettings
寫法一
語法
var credential = MongoCredential.CreateCredential(null, "{username}", "{password}"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = credential, Servers = new List<MongoServerAddress>(){ new MongoServerAddress("{host1}", {port1}), new MongoServerAddress("{host2}", {port2}), new MongoServerAddress("{host3}", {port3}) }, ReplicaSetName="{replicaSet Name}" });
實際範例
var credential = MongoCredential.CreateCredential(null, "yowkoTest", "passwordTest"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = credential, Servers = new List<MongoServerAddress>(){ new MongoServerAddress("localhost", 27017), new MongoServerAddress("localhost", 27027), new MongoServerAddress("localhost", 27037) }, ReplicaSetName="TestReplicaSet" });
寫法二
語法
MongoIdentity identity = new MongoInternalIdentity("{Database}", "{username}"); MongoIdentityEvidence evidence = new PasswordEvidence("{password}"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = new MongoCredential(null, identity, evidence), Servers = new List<MongoServerAddress>(){ new MongoServerAddress("{host1}", {port1}), new MongoServerAddress("{host2}", {port2}), new MongoServerAddress("{host3}", {port3}) }, ReplicaSetName="{replicaSet Name}" });
實際範例
MongoIdentity identity = new MongoInternalIdentity("test", "yowkoTest"); MongoIdentityEvidence evidence = new PasswordEvidence("passwordTest"); MongoClient client = new MongoClient( new MongoClientSettings { Credential = new MongoCredential(null, identity, evidence), Servers = new List<MongoServerAddress>(){ new MongoServerAddress("localhost", 27017), new MongoServerAddress("localhost", 27027), new MongoServerAddress("localhost", 27037) }, ReplicaSetName="TestReplicaSet" });
使用 MongoUrl
語法
MongoClient client = new MongoClient( MongoUrl.Create("mongodb://{username}:{password}@{host1}:{port1},{host2}:{port2},{host3}:{port3}/?replicaSet={replicaSet Name}"));
實際範例
MongoClient client = new MongoClient( MongoUrl.Create("mongodb://yowko:[email protected]:27017,127.0.0.1:27027,127.0.0.1:27037/?replicaSet=TestReplicaSet"));
心得
原本只是沒營養地紀錄一下原本預估一 ~ 二小時可以搞定,出乎意料地花了一整天的時間,之前我就有一樣的感覺:對於 MongoDB 常常覺得不用紀錄的東西,但下次真的要用時又要花不少時間摸索,希望再加上這次經驗後 下次使用起來會順手些
參考資訊
文章作者 Yowko Tsai
上次更新 2021-11-02
授權合約
本部落格 (Yowko's Notes) 所有的文章內容(包含圖片),任何轉載行為,必須通知並獲本部落格作者 (Yowko Tsai) 的同意始得轉載,且轉載皆須註明出處與作者。
Yowko's Notes 由 Yowko Tsai 製作,以創用CC 姓名標示-非商業性-相同方式分享 3.0 台灣 授權條款 釋出。