网站地图    收藏   

主页 > 后端 > MongoDB >

c#操作mongodb插入数据效率

来源:自学PHP网    时间:2019-08-01 18:22 作者:小飞侠 阅读:

[导读] c#操作mongodb插入数据效率...

mongodb的数据插入速度是其一个亮点,同样的10000条数据,插入的速度要比Mysql和sqlserver都要快,当然这也是要看使用者怎么个使用法,你代码如果10000次写入使用10000次连接,那也是比不过其他数据库使用事务一次性提交的速度的。

InsertManyAsync()这个方法带入的参数只要是实现了IEnumerable接口的类型就可以,所以可是list<>,这样的数据类型;

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 IMongoCollection collection
    {
      get {
        return _collection;
      }
      set {
        _collection = value;
      }
    }
    protected static IMongoClient _client;
    protected static IMongoDatabase _database;
    protected static IMongoCollection _collection;
     //测试效率,两个方法用时比较
    public async static void TestMongo()
    {
      //自定义的对象
      RoomInfo roomdata = new RoomInfo();
      List docunemts = new List();
      collection = database.GetCollection("HotelPersonInfo");
      Stopwatch sw = new Stopwatch();
      sw.Start();
      for (int i = 1; i < 10000; i++)
      {
         //mongo对用户自定义的对象扩展了tobasonDocument这个方法,可直接用
        var roomdatadocument = new BsonDocument(roomdata.ToBsonDocument());
        docunemts.Add(roomdatadocument);
      }
      //一次10000条
       //这方法 查看api手册,只要实现了IEnumerable借口的类型就都行
      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.GetCollection("HotelPersonInfo");
      // collection.InsertOneAsync(roomdatadocument);
    }
  }
}
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; }
    /// 
    /// 名字
    /// 
    public string Name { get; set; }      
     /// 
    /// 手机号码
    /// 
    public string Moblie { get; set; }
    /// 
    /// 邮箱
    /// 
    public string EMail {get;set;}
    /// 
    /// 座机
    /// 
    public string Tel { get; set; }
    /// 
    /// 传真
    /// 
    public string Fax { get; set; }
    /// 
    /// 身份证
    /// 
    public string IdentityId { get; set; }
     /// 
    /// 使用什么注册的
    /// ID --身份证 (只需要id身份证的信息)
    /// 
    public string RegisterType { get; set; }
      /// 
    /// 会员卡号
    /// 
    public string CardNo { get; set; }
    /// 
    /// 性别
    /// 
    public string Sex { get; set; }
    /// 
    /// 生日
    /// 
    public string Birthday { get; set; }
    /// 
    /// 地址
    /// 
    public string Address { get; set; }
    /// 
    /// 邮编
    /// 
    public string ZipCode { get; set; }
    public string District2 { get; set; }
    public string District3 { get; set; }
    public string District4 { get; set; }
    /// 
    /// 注册时间 
    /// 
    public string RegisterDate { get; set; }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自学php网的支持。如果你想了解更多相关内容请查看下面相关链接

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论