如何使用官方c#驱动程序在MongoDB中使用Update.Set更新多个字段?

时间:2022-02-09 20:22:09

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?

以下代码将允许我更新FirstName =“john”和LastName =“Doe”的电子邮件。如何在不使用Save()方法的情况下更新电子邮件和电话?

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);

4 个解决方案

#1


66  

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");

#2


23  

You can also use the generic and type-safe Update<TDocument>:

您还可以使用通用和类型安全的Update

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

#3


3  

var _personobj = _person
{
    Id = 10, // Object ID
    Email="jdoe@gmail.com",
    Phone=123456,

};
var replacement = Update<_person>.Replace(_personobj);
collection.Update(myquery, replacement);

#4


1  

if you want to one more update document's field then pick multi flag.

如果你想再一个更新文档的字段,那么选择多标志。

for example mongodb 2.0 or 3.0v:

例如mongodb 2.0或3.0v:

yourCollection.Update(_filter
                      , _update
                      , new MongoUpdateOptions() { Flags = UpdateFlags.Multi })  

#1


66  

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");

#2


23  

You can also use the generic and type-safe Update<TDocument>:

您还可以使用通用和类型安全的Update

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

#3


3  

var _personobj = _person
{
    Id = 10, // Object ID
    Email="jdoe@gmail.com",
    Phone=123456,

};
var replacement = Update<_person>.Replace(_personobj);
collection.Update(myquery, replacement);

#4


1  

if you want to one more update document's field then pick multi flag.

如果你想再一个更新文档的字段,那么选择多标志。

for example mongodb 2.0 or 3.0v:

例如mongodb 2.0或3.0v:

yourCollection.Update(_filter
                      , _update
                      , new MongoUpdateOptions() { Flags = UpdateFlags.Multi })