private void Query_Click(object sender, EventArgs e) { var database = new GetMongoDB().GetDB(); IMongoCollection <users> collection = database.GetCollection <users>("users"); try { var result = collection.Find <users>(t => t.id == "45").FirstOrDefault(); string name = result.name.ToString(); MessageBox.Show(name); } catch (Exception) { MessageBox.Show("查询失败"); } }
private void Update_Click(object sender, EventArgs e) { var database = new GetMongoDB().GetDB(); IMongoCollection <users> collection = database.GetCollection <users>("users"); UpdateDefinition <users> definition = "{$set:{'name':'更新测试','password':'******','age':32,'createtime':'" + DateTime.Now + "'}}"; var result = collection.UpdateOne <users>(t => t.id == "45", definition); if (result.ModifiedCount > 0) { MessageBox.Show("更新成功"); } else { MessageBox.Show("更新失败"); } }
private void button7_Click(object sender, EventArgs e) { var database = new GetMongoDB().GetDB(); IMongoCollection <users> collection = database.GetCollection <users>("users"); UpdateDefinition <users> definition = "{$set:{'name':'mongodbupdate','password':'******','age':32,'createtime':'" + DateTime.Now + "'}}"; Stopwatch sw = new Stopwatch(); sw.Start(); var result = collection.UpdateOne <users>(t => t.id == "9999", definition); sw.Stop(); var temp = sw.Elapsed; string time = "使用mongodb单条更新100W数据总耗时为:" + temp.ToString(); Console.WriteLine(time); }
private void button12_Click(object sender, EventArgs e) { List <users> listModel = new List <users>(); int count = 1000; for (int j = 0; j < count; j++) { users model = new users(); model.id = Guid.NewGuid().ToString(); model.name = "mongodb"; model.password = "******"; model.age = j; model.createtime = DateTime.Now; listModel.Add(model); } var database = new GetMongoDB().GetDB(); IMongoCollection <users> collection = database.GetCollection <users>("users"); int n = 100; Stopwatch sw = new Stopwatch(); Task[] tasks = new Task[n]; sw.Start(); for (int i = 0; i < n; i++) { tasks[i] = Task.Factory.StartNew(() => { foreach (var item in listModel) { lock (o1) { collection.InsertOne(item); } } }); } Task.WaitAll(tasks); sw.Stop(); var temp = sw.Elapsed; string time = "100个并发连接对非关系型数据库mongodb批量插入100W数据总耗时为:" + temp.ToString(); Console.WriteLine(time); }
private void Insert_Click(object sender, EventArgs e) { var database = new GetMongoDB().GetDB(); users model = new users() { id = "45", name = "form窗体测试新增", password = "******", age = 24, createtime = DateTime.Now }; IMongoCollection <users> collection = database.GetCollection <users>("users"); try { collection.InsertOne(model); MessageBox.Show("新增成功"); } catch (Exception) { MessageBox.Show("新增失败"); } }