mongodb的數(shù)據(jù)插入速度是其一個(gè)亮點(diǎn),同樣的10000條數(shù)據(jù),插入的速度要比Mysql和sqlserver都要快,當(dāng)然這也是要看使用者怎么個(gè)使用法,你代碼如果10000次寫(xiě)入使用10000次連接,那也是比不過(guò)其他數(shù)據(jù)庫(kù)使用事務(wù)一次性提交的速度的。
同樣,mongo也提供的一次性插入巨量數(shù)據(jù)的方法,因?yàn)閙ongodb沒(méi)有事務(wù)這回事,所以在在C#驅(qū)動(dòng)里,具體方法是InsertManyAsync()一次性插入多個(gè)文檔。與之對(duì)應(yīng)的是InsertOneAsync,這個(gè)是一次插入一個(gè)文檔;
InsertManyAsync()這個(gè)方法帶入的參數(shù)只要是實(shí)現(xiàn)了IEnumerable接口的類(lèi)型就可以,所以可是list>,這樣的數(shù)據(jù)類(lèi)型;
同樣的10000次插入,兩個(gè)方法時(shí)間差別很大。如圖:
使用一次性插入多個(gè)文檔方法,插入10000條耗時(shí)僅1.3秒,分成10000次插入,耗時(shí)19.9秒。區(qū)別大了個(gè)去。同樣,前面我做過(guò)使用mysql插入10000條記錄,要用4秒多,可見(jiàn),這mongodb插入速度不是吹 的。
具體的代碼如下,貼上:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Diagnostics;
namespace sqltomongo
{
public class MongoHelp
{
private static IMongoClient client
{
get
{
if (null == _client)
{
_client = new MongoClient("mongodb://127.0.0.1:27017");
}
return _client;
}
}
public static IMongoDatabase database
{
get {
_database = client.GetDatabase("HotelPersonInfo");
return _database;
}
set {
_database = value;
}
}
public static IMongoCollectionBsonDocument> collection
{
get {
return _collection;
}
set {
_collection = value;
}
}
protected static IMongoClient _client;
protected static IMongoDatabase _database;
protected static IMongoCollectionBsonDocument> _collection;
//測(cè)試效率,兩個(gè)方法用時(shí)比較
public async static void TestMongo()
{
//自定義的對(duì)象
RoomInfo roomdata = new RoomInfo();
ListBsonDocument> docunemts = new ListBsonDocument>();
collection = database.GetCollectionBsonDocument>("HotelPersonInfo");
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 1; i 10000; i++)
{
//mongo對(duì)用戶(hù)自定義的對(duì)象擴(kuò)展了tobasonDocument這個(gè)方法,可直接用
var roomdatadocument = new BsonDocument(roomdata.ToBsonDocument());
docunemts.Add(roomdatadocument);
}
//一次10000條
//這方法 查看api手冊(cè),只要實(shí)現(xiàn)了IEnumerable借口的類(lèi)型就都行
await collection.InsertManyAsync(docunemts);
sw.Stop();
TimeSpan ts2 =sw.Elapsed;
Console.WriteLine("total is " + ts2.TotalMilliseconds);
///一次次插 10000次
Stopwatch sw2 = new Stopwatch();
sw2.Start();
for (int i = 1; i 10000; i++)
{
var roomdatadocument = new BsonDocument(roomdata.ToBsonDocument());
await collection.InsertOneAsync(roomdatadocument);
}
sw2.Stop();
TimeSpan ts22 = sw2.Elapsed;
Console.WriteLine("total is " + ts22.TotalMilliseconds);
// await collection.InsertOneAsync(roomdatadocument);
//collection = database.GetCollectionBsonDocument>("HotelPersonInfo");
// collection.InsertOneAsync(roomdatadocument);
}
}
}
里面使用了一個(gè)自定義的對(duì)象:
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
namespace sqltomongo
{
public class RoomInfo
{
public RoomInfo()
{
// id = "test";
Name = "nafd"; Moblie = "123456"; EMail = "dd@qq.com"; Tel = "010123"; Fax = "0755-001";
IdentityId = "616112323231"; RegisterType = "tid"; CardNo = "cardno"; Sex = "男"; Birthday = "1999";
Address = "china beijing"; ZipCode = "519000"; RegisterDate = "2015-03-03";
District2 = "District2";
District3 = "District3";
District4 = "District4";
}
// public string id { get; set; }
/// summary>
/// 名字
/// /summary>
public string Name { get; set; }
/// summary>
/// 手機(jī)號(hào)碼
/// /summary>
public string Moblie { get; set; }
/// summary>
/// 郵箱
/// /summary>
public string EMail {get;set;}
/// summary>
/// 座機(jī)
/// /summary>
public string Tel { get; set; }
/// summary>
/// 傳真
/// /summary>
public string Fax { get; set; }
/// summary>
/// 身份證
/// /summary>
public string IdentityId { get; set; }
/// summary>
/// 使用什么注冊(cè)的
/// ID --身份證 (只需要id身份證的信息)
/// /summary>
public string RegisterType { get; set; }
/// summary>
/// 會(huì)員卡號(hào)
/// /summary>
public string CardNo { get; set; }
/// summary>
/// 性別
/// /summary>
public string Sex { get; set; }
/// summary>
/// 生日
/// /summary>
public string Birthday { get; set; }
/// summary>
/// 地址
/// /summary>
public string Address { get; set; }
/// summary>
/// 郵編
/// /summary>
public string ZipCode { get; set; }
public string District2 { get; set; }
public string District3 { get; set; }
public string District4 { get; set; }
/// summary>
/// 注冊(cè)時(shí)間
/// /summary>
public string RegisterDate { get; set; }
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
您可能感興趣的文章:- mongodb使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo
- C#簡(jiǎn)單操作MongoDB的步驟全紀(jì)錄
- C#中如何將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類(lèi)的方法總結(jié)
- c# 插入數(shù)據(jù)效率測(cè)試(mongodb)
- Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例
- 詳解MongoDB for C#基礎(chǔ)入門(mén)
- MongoDB實(shí)現(xiàn)基于關(guān)鍵詞的文章檢索功能(C#版)
- C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡(jiǎn)單例子
- MongoDB入門(mén)教程之C#驅(qū)動(dòng)操作實(shí)例
- C# 對(duì)MongoDB 進(jìn)行增刪改查的簡(jiǎn)單操作實(shí)例
- 關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法
- C# 操作 MongoDB的示例demo