這篇文章主要介紹mongodb如何使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
目前創(chuàng)新互聯(lián)建站已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、欽北網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。Mongodb提供了多種開(kāi)發(fā)語(yǔ)言的驅(qū)動(dòng),java,python,c++,c# 等,這里選用c#驅(qū)動(dòng)作為測(cè)試;
首先上mongo官網(wǎng)下載驅(qū)動(dòng)。Ps:官方網(wǎng)站經(jīng)常連接不順利。
還不如直接在vs的nuget管理包中搜索mongoDB.driver.
需要引入的命名空間:
using MongoDB.Bson; using MongoDB.Driver;
Driver是驅(qū)動(dòng)核心,Bson是和數(shù)據(jù)格式相關(guān)的;
定義一個(gè)mongo客戶(hù)端,一個(gè)mongodb,一個(gè)數(shù)據(jù)集合;
protected staticIMongoClient client; protected staticIMongoDatabase database; protected staticIMongoCollection<BsonDocument> collection;
連接上MongoDB
//定義連接 client = new MongoClient("mongodb://127.0.0.1:27017"); //獲取test數(shù)據(jù)庫(kù) database = client.GetDatabase("test"); //獲取test數(shù)據(jù)庫(kù)中的集合bios collection = database.GetCollection<BsonDocument>("bios");
這里解釋說(shuō)明下:首先你得讓mongod(mongo的服務(wù)端)運(yùn)行起來(lái),不然服務(wù)端都沒(méi)開(kāi),怎么連接呢;目前測(cè)試還沒(méi)有涉及到安全以及用戶(hù)權(quán)限數(shù)據(jù)庫(kù)管理這塊,所以這里的連接都是使用的默認(rèn)不帶用戶(hù)登錄驗(yàn)證;
需求注意的是,如果我們建立的是控制臺(tái)程序,那么這個(gè)連接必須寫(xiě)地址必須帶端口,就像上面所寫(xiě);
如果是建立的一個(gè)MVC web,你僅僅是測(cè)試數(shù)據(jù)插入,在這種無(wú)安全驗(yàn)證的方式下,你可以省去連接字符串。
如下圖;
接下來(lái)就是定義一個(gè)測(cè)試數(shù)據(jù):
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" } };
最后調(diào)用InsertOneAsync()
方法;
collection.InsertOneAsync(document);
最終插入結(jié)果:
這里使用shell來(lái)看數(shù)據(jù)的話(huà)就太不直觀(guān)了,這里使用的是比較常用的一個(gè)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(); } } }
以上是“mongodb如何使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:mongodb如何使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo-創(chuàng)新互聯(lián)
鏈接地址:http://m.rwnh.cn/article38/dddssp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、App設(shè)計(jì)、定制網(wǎng)站、搜索引擎優(yōu)化、標(biāo)簽優(yōu)化、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容