好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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 ("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测试数据"; 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网的支持。如果你想了解更多相关内容请查看下面相关链接

查看更多关于c#操作mongodb插入数据效率的详细内容...

  阅读:78次