Operators
Set operator - $set
The $set operator is used to update the value of a specified field.
Builders<T<.Update.Set(doc => doc.<field>, <value>)
The sample updates the FirstName field of the first document in the collection.
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// create a Set operator update definition
var updateNameDefinition = Builders<User>.Update
.Set(u => u.FirstName, "Chris");
// update the document
var updateNameResult = await collection
.UpdateOneAsync(firstUserFilter,
updateNameDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonUpdateNameDefinition = Builders<BsonDocument>
.Update.Set("firstName", "John");
var bsonUpdateNameResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonUpdateNameDefinition);
db.users.updateOne({},
{ $set: { firstName: "Chris" }});
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
Multiple fields update
You can update multiple document's fields in one operation by declaring more than one update definitions.
The sample updates the first document's Phone, Website and FavoriteSports (array field).
var collection = database
.GetCollection<User>(Constants.UsersCollection);
var multiUpdateDefinition = Builders<User>.Update
.Set(u => u.Phone, "123-456-789")
.Set(u => u.Website, "https://chsakell.com")
.Set(u => u.FavoriteSports,
new List<string> { "Soccer", "Basketball" });
var multiUpdateResult = await collection
.UpdateOneAsync(firstUserFilter,
multiUpdateDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonMultiUpdateDefinition = Builders<BsonDocument>.Update
.Set("phone", "123-456-789")
.Set("website", "https://chsakell.com")
.Set("favoriteSports",
new List<string> { "Soccer", "Basketball" });
var bsonMultiUpdateResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonMultiUpdateDefinition);
db.users.updateOne({}, { $set: {
phone: "123-456-789",
website: "https://chsakell.com",
favoriteSports: ["Soccer", "Basketball"]
}});
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
Inc operator - $inc
The $inc operator is used to increase the value of a specified field by a specified amount.
Builders<T<.Update.Inc(doc => doc.<field>, <value>)
The sample increments the first document's salary.
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// create an $inc update definition
var incrementSalaryDefinition = Builders<User>
.Update.Inc(u => u.Salary, 450);
var incrementSalaryResult = await collection
.UpdateOneAsync(firstUserFilter,
incrementSalaryDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonIncrementSalaryDefinition = Builders<BsonDocument>
.Update.Inc("salary", 450);
var bsonIncrementSalaryResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonIncrementSalaryDefinition);
db.users.updateOne({}, { $inc:
{ salary: NumberDecimal("450.00") }});
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
The $inc operator can be used with negative values to decrease a specified field's value
Min operator - $min
The $min operator is used to update the value of a specified field only if the new value is less than the current value.
Builders<T<.Update
.Min(doc => doc.<field>, <value>)
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// preparation - set current salary to 3000
// for demo only
await collection.UpdateOneAsync(firstUserFilter, Builders<User>
.Update.Set(u => u.Salary, 3000));
// update only if the new value is less than the current
var minUpdateDefinition = Builders<User>
.Update.Min(u => u.Salary, 2000);
// 2000 is less than 3000 so update succeeds
var minUpdateResult = await collection
.UpdateOneAsync(firstUserFilter, minUpdateDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
await bsonCollection.UpdateOneAsync(bsonFirstUserFilter,
Builders<BsonDocument>.Update.Set("salary", 3000));
// would not update if the new salary was > 3000
var bsonMinUpdateDefinition = Builders<BsonDocument>
.Update.Min("salary", 2000);
var bsonMinUpdateResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonMinUpdateDefinition);
db.users.updateOne({}, { $min: {
salary: NumberDecimal("2000") } })
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
Of course if the new value is equal to the current one, the update result will return that no documents updated.
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 0
}
Max operator - $max
The $max operator is used to update the value of a specified field only if the new value is greater than the current value.
Builders<T<.Update
.Max(doc => doc.<field>, <value>)
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// preparation - set current salary to 3000
// for demo only
await collection.UpdateOneAsync(firstUserFilter, Builders<User>
.Update.Set(u => u.Salary, 3000));
// update only if the new value is greater than the current
var maxUpdateDefinition = Builders<User>
.Update.Max(u => u.Salary, 3500);
// 3500 is greater than 3000 so update succeeds
var maxUpdateResult = await collection
.UpdateOneAsync(firstUserFilter, maxUpdateDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
await bsonCollection.UpdateOneAsync(bsonFirstUserFilter,
Builders<BsonDocument>.Update.Set("salary", 3000));
// would not update if the new salary was < 3500
var bsonMaxUpdateDefinition = Builders<BsonDocument>
.Update.Max("salary", 3500);
var bsonMaxUpdateResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonMaxUpdateDefinition);
db.users.updateOne({}, { $max: {
salary: NumberDecimal("3500") }
})
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
Mul operator - $mul
The $mul operator is used to multiply the current value of a specified field by a specified value.
Builders<T<.Update.Mul(doc => doc.<field>, <value>)
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// preparation - set current salary to 1000
// for demo only
await collection.UpdateOneAsync(firstUserFilter,
Builders<User>.Update.Set(u => u.Salary, 1000));
// multiply the current salary value by 2
var mulUpdateDefinition = Builders<User>
.Update.Mul(u => u.Salary, 2);
var mulUpdateResult = await collection
.UpdateOneAsync(firstUserFilter, mulUpdateDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
await bsonCollection.UpdateOneAsync(bsonFirstUserFilter,
Builders<BsonDocument>.Update
.Set("salary", 1000));
var bsonMulUpdateDefinition = Builders<BsonDocument>
.Update.Mul("salary", 2);
var bsonMulUpdateResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonMulUpdateDefinition);
db.users.updateOne({}, { $mul: { salary: 2 } })
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
Unset operator - $unset
The $unset operator is used to remove a field from a document.
Builders<T<.Update.Unset(doc => doc.<field>)
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// remove the website field
var removeWebsiteDefinition = Builders<User>
.Update.Unset(u => u.Website);
var removeWebsiteFieldUpdateResult = await collection
.UpdateOneAsync(firstUserFilter,
removeWebsiteDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonRemoveWebsiteDefinition = Builders<BsonDocument>
.Update.Unset("website");
var bsonRemoveWebsiteFieldUpdateResult =
await bsonCollection.UpdateOneAsync(bsonFirstUserFilter,
bsonRemoveWebsiteDefinition);
db.users.updateOne({}, { $unset: { website: "" }})
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
// user document
{
"_id" : ObjectId("5e9d578781f29c3b1c22d796"),
"gender" : 1,
"firstName" : "Tracey",
"lastName" : "Pfannerstill",
"userName" : "Tracey_Pfannerstill97",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/mattlat/128.jpg",
"email" : "Tracey_Pfannerstill99@yahoo.com",
"dateOfBirth" : ISODate("1960-02-09T21:33:43.279+02:00"),
"address" : {
"street" : "636 Klein Corners",
"suite" : "Apt. 556",
"city" : "Kacieport",
"state" : "New Mexico",
"zipCode" : "58913-4315",
"geo" : {
"lat" : -48.7019,
"lng" : -111.2376
}
},
"phone" : "320-280-8093 x4487",
"website" : "josh.com",
"company" : {
"name" : "Sawayn - Donnelly",
"catchPhrase" : "Customer-focused mobile groupware",
"bs" : "maximize cross-media e-tailers"
},
"salary" : 1716,
"monthlyExpenses" : 2203,
"favoriteSports" : [
"Cricket",
"Formula 1",
"Table Tennis",
"Water Polo",
"Basketball",
"Handball",
"American Football"
],
"profession" : "Financial Adviser"
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
ββ β‘ Danger β β‘ββ Danger β β‘ββ Danger β β‘
Use caution when applying the Unset operator!! In case you unset a non nullable property then you might end up having unexpected results. For example, if you unset the salary field on a user document, which is a decimal field, then the next time you read that document, the salary will be 0, not null!
// remove salary field - decimal property
var removeSalaryDefinition = Builders<User>
.Update.Unset(u => u.Salary);
var removeSalaryFieldUpdateResult =
await collection.UpdateOneAsync(firstUserFilter,
removeSalaryDefinition);
// firstUser.Salary is equal to 0!
var firstUser = await collection.Find(firstUserFilter)
.FirstOrDefaultAsync();
Rename operator - $rename
The $rename operator is used to rename a field.
Builders<T<.Update
.Rename(doc => doc.<field>, <new-name>)
The sample updates the Phone field of the first document to PhoneNumber using the $rename operator.
var collection = database
.GetCollection<User>(Constants.UsersCollection);
// create an empty filter
var firstUserFilter = Builders<User>.Filter.Empty;
// rename the phone field to phoneNumber for the first user
var renamePhoneDefinition = Builders<User>.
Update.Rename(u => u.Phone, "phoneNumber");
// update the document
var renamePhoneFieldUpdateResult = await collection
.UpdateOneAsync(firstUserFilter,
renamePhoneDefinition);
var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
var bsonRenamePhoneDefinition = Builders<BsonDocument>
.Update.Rename("phone", "phoneNumber");
var bsonRenamePhoneFieldUpdateResult = await bsonCollection
.UpdateOneAsync(bsonFirstUserFilter,
bsonRenamePhoneDefinition);
db.users.updateOne({}, {
$rename: { phone: "phoneNumber" }
})
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}
public class User
{
[BsonId]
[BsonIgnoreIfDefault] // required for replace documents
public ObjectId Id { get; set; }
public Gender Gender { get; set; }
public string FirstName {get; set; }
public string LastName {get; set; }
public string UserName {get; set; }
public string Avatar {get; set; }
public string Email {get; set; }
public DateTime DateOfBirth {get; set; }
public AddressCard Address {get; set; }
public string Phone {get; set; }
[BsonIgnoreIfDefault]
public string Website {get; set; }
public CompanyCard Company {get; set; }
public decimal Salary { get; set; }
public int MonthlyExpenses { get; set; }
public List<string> FavoriteSports { get; set; }
public string Profession { get; set; }
}
When renaming document fields make sure the new field names can be matched back you your C# models. You can control the field's name in the database using the[BsonElement]
attribute. In the following example, the Phone property will be saved as phoneNumber in the database while can be deserialized back to the Phone property properly.
[BsonElement("phoneNumber")]
public string Phone {get; set; }
Last updated