如何使用官方的C#的驱动程序在MongoDB中使用Update.Set你更新多个领域? [英] How do you update multiple field using Update.Set in MongoDB using official c# driver?

查看:1022
本文介绍了如何使用官方的C#的驱动程序在MongoDB中使用Update.Set你更新多个领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code将使我更新的电子邮件,其中姓=约翰和姓氏=李四。你如何在不使用Save()方法更新电子邮件和电话?

  MongoDB.Driver.MongoServer _ SERVER = MongoDB.Driver.MongoServer.Create(MongoDB的:// localhost的);
MongoDB.Driver.MongoDatabase _dataBase = _server.GetDatabase(测试);
MongoDB.Driver.MongoCollection<人> _person = _dataBase.GetCollection<人>(人);

//开创全省新的人,并将其插入集合
的ObjectId NEWID = ObjectId.GenerateNewId();
人newPerson =新的Person();
newPerson.Id = newId.ToString();
newPerson.FirstName =约翰;
newPerson.LastName =李四;
newPerson.Email =john.doe@gmail.com;
newPerson.Phone =8005551222;
_person.Insert(newPerson);

//更新电话和电子邮件与姓约翰和姓氏母鹿所有记录
MongoDB.Driver.Builders.QueryComplete更改为MyQuery = MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ(名字,约翰),MongoDB.Driver.Builders.Query.EQ(姓氏,Doe的));
MongoDB.Driver.Builders.UpdateBuilder更新= MongoDB.Driver.Builders.Update.Set(电子邮件,jdoe@gmail.com);

_person.Update(更改为MyQuery,更新);
 

解决方案

这很简单;),只需添加另一套或一些其他操作到您的更新:

  VAR更新= Update.Set(电子邮件,jdoe@gmail.com)
                    。设置(电话,4455512);
 

The following code will allow me to update the Email where FirstName = "john" and LastName = "Doe". How do you update both Email and Phone without using Save() method?

MongoDB.Driver.MongoServer _server = MongoDB.Driver.MongoServer.Create("mongodb://localhost");
MongoDB.Driver.MongoDatabase _dataBase = _server.GetDatabase("test");
MongoDB.Driver.MongoCollection<Person> _person = _dataBase.GetCollection<Person>("person");

//Creat new person and insert it into collection
ObjectId newId  = ObjectId.GenerateNewId();
Person newPerson = new Person();
newPerson.Id = newId.ToString();
newPerson.FirstName = "John";
newPerson.LastName = "Doe";
newPerson.Email = "john.doe@gmail.com";
newPerson.Phone = "8005551222";
_person.Insert(newPerson);

//Update phone and email for all record with firstname john and lastname doe
MongoDB.Driver.Builders.QueryComplete myQuery = MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ("FirstName", "John"),    MongoDB.Driver.Builders.Query.EQ("LastName", "Doe"));
MongoDB.Driver.Builders.UpdateBuilder update = MongoDB.Driver.Builders.Update.Set("Email", "jdoe@gmail.com");

_person.Update(myQuery, update);

解决方案

It's very simple ;), just add another set or some else operation to the your update:

 var update = Update.Set("Email", "jdoe@gmail.com")
                    .Set("Phone", "4455512");

这篇关于如何使用官方的C#的驱动程序在MongoDB中使用Update.Set你更新多个领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆