[英]Mongodb c sharp driver document update error
I am using c# mongo db driver to insert and update a document.我正在使用 c# mongo db 驱动程序来插入和更新文档。 First i insert a record and then I try to update a specific field of record using FindOneAndUpdateAsync method.
首先我插入一条记录,然后尝试使用 FindOneAndUpdateAsync 方法更新特定的记录字段。 I am able to update it properly but after update I am getting a error "Required element 'Name' for property 'Name' of class SampleApp.Person is missing."
我能够正确更新它,但更新后出现错误“class SampleApp.Person 的属性‘Name’的必需元素‘Name’丢失。” I am using MongoCommunity edition 4.4.1 and mongo driver is 2.11.4.
我使用的是 MongoCommunity 版本 4.4.1,mongo 驱动程序是 2.11.4。
I am attaching image of mongo compass view for your reference.我附上 mongo 指南针视图的图像供您参考。
My collection structure is我的收藏结构是
[BsonSerializer(typeof(ImpliedImplementationInterfaceSerializer<IPerson, Person>))]
public interface IPerson
{
string Id { get; set; }
string Name { get; set; }
int Age { get; set; }
string Email { get; set; }
IAddress Address { get; set; }
bool IsActive { get; set; }
}
public class Person : IPerson
{
[BsonId]
public string Id { get; set; }
[BsonRequired]
public string Name { get; set; }
[BsonRequired]
public int Age { get; set; }
public string Email { get; set; }
public IAddress Address { get; set; }
[BsonRequired]
public bool IsActive { get; set; }
}
public interface IAddress
{
string Line1 { get; set; }
string Line2 { get; set; }
string Line3 { get; set; }
string City { get; set; }
string State { get; set; }
string Pincode { get; set; }
}
public class Address : IAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Pincode { get; set; }
}
My code to insert and update is as shown below.我的插入和更新代码如下所示。
MongoClient client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
BsonClassMap.RegisterClassMap<Address>();
var peopleCollection = database.GetCollection<IPerson>("People");
IPerson person = new Person()
{
Age = 50,
Email = "a@a.com",
IsActive = false,
Name = "David",
Id = ObjectId.GenerateNewId().ToString()
};
try
{
await peopleCollection.InsertOneAsync(person);
IAddress address = new Address()
{
City = "Bengaluru",
Line1 = "Bengaluru",
Line2 = "Bengaluru",
Line3 = "Bengaluru",
Pincode = "560001",
State = "KA"
};
var updateDefinition = Builders<IPerson>.Update.Set(x => x.Address, address).Set(x =>
x.IsActive, true);
var filter = Builders<IPerson>.Filter.Eq(x => x.Email, "a@a.com");
FindOneAndUpdateOptions<IPerson> updateOptions = new FindOneAndUpdateOptions<IPerson>
{
IsUpsert = false,
ReturnDocument = ReturnDocument.After,
Projection = Builders<IPerson>.Projection.Include(c => c.Address)
};
IPerson updatedPerson = await peopleCollection.FindOneAndUpdateAsync(filter, updateDefinition, updateOptions);
}
catch (Exception ex)
{
string s = ex.Message;
Console.WriteLine(s);
}
My guess is that the value pre-update, does not have the Name property at all, so when the FindOneAndUpdateAsync
gets the data and tries to deserialize it (before updating) it fails with that error.我的猜测是更新前的值根本没有 Name 属性,因此当
FindOneAndUpdateAsync
获取数据并尝试反序列化它(在更新之前)时,它会因该错误而失败。
Two solutions for this两种解决方案
[BsonRequired]
so that youy avoid this error.[BsonRequired]
以避免此错误。Name
property, before running your code.Name
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.