当前位置: 移动技术网 > IT编程>数据库>MongoDB > c#操作mongodb插入数据效率

c#操作mongodb插入数据效率

2019年05月28日  | 移动技术网IT编程  | 我要评论

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

同样,mongo也提供的一次性插入巨量数据的方法,因为mongodb没有事务这回事,所以在在c#驱动里,具体方法是insertmanyasync()一次性插入多个文档。与之对应的是insertoneasync,这个是一次插入一个文档;

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

同样的10000次插入,两个方法时间差别很大。如图:

使用一次性插入多个文档方法,插入10000条耗时仅1.3秒,分成10000次插入,耗时19.9秒。区别大了个去。同样,前面我做过使用mysql插入10000条记录,要用4秒多,可见,这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 imongocollection<bsondocument> collection
    {
      get {
        return _collection;
      }
      set {
        _collection = value;
      }
    }
    protected static imongoclient _client;
    protected static imongodatabase _database;
    protected static imongocollection<bsondocument> _collection;
     //测试效率,两个方法用时比较
    public async static void testmongo()
    {
      //自定义的对象
      roominfo roomdata = new roominfo();
      list<bsondocument> docunemts = new list<bsondocument>();
      collection = database.getcollection<bsondocument>("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<bsondocument>("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; }
    /// <summary>
    /// 名字
    /// </summary>
    public string name { get; set; }      
     /// <summary>
    /// 手机号码
    /// </summary>
    public string moblie { get; set; }
    /// <summary>
    /// 邮箱
    /// </summary>
    public string email {get;set;}
    /// <summary>
    /// 座机
    /// </summary>
    public string tel { get; set; }
    /// <summary>
    /// 传真
    /// </summary>
    public string fax { get; set; }
    /// <summary>
    /// 身份证
    /// </summary>
    public string identityid { get; set; }
     /// <summary>
    /// 使用什么注册的
    /// id --身份证 (只需要id身份证的信息)
    /// </summary>
    public string registertype { get; set; }
      /// <summary>
    /// 会员卡号
    /// </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>
    /// 注册时间 
    /// </summary>
    public string registerdate { get; set; }
  }
}

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网