MongoDB C# Treiber: Das Update hat nur Felder bereitgestellt, einschließlich der Festlegung einiger Felder auf NULLC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 MongoDB C# Treiber: Das Update hat nur Felder bereitgestellt, einschließlich der Festlegung einiger Felder auf NULL

Post by Anonymous »

Ich verwende MongoDB mit C# (MongoDB.Driver) und möchte nur die in meinem API -Modell bereitgestellten Felder aktualisieren. Mein aktueller Ansatz eignet sich für die Aktualisierung von Werten, aber ich kann kein Feld (Zeichenfolge, Nummer oder ObjektID) explizit auf Null festlegen.var updateBuilder = Builders.Update;
var updates = new List();

foreach (var prop in typeof(ExportShipmentExBond).GetProperties())
{
if (!prop.CanRead) continue;

if (prop.Name == nameof(ExportShipmentExBond.ExportShipmentExBondId) ||
prop.Name == nameof(ExportShipmentExBond.ExportShipmentExBondCreatedDate) ||
prop.Name == nameof(ExportShipmentExBond.ExportShipmentExBondCreatedBy))
continue;

var newValue = prop.GetValue(model);

if (newValue != null)
{
updates.Add(updateBuilder.Set(prop.Name, newValue));
}
}

if (updates.Count > 0)
{
var updateDefinition = updateBuilder.Combine(updates);
await _collection.FindOneAndUpdateAsync(
filter,
updateDefinition,
new FindOneAndUpdateOptions
{
ReturnDocument = ReturnDocument.After
});
}
< /code>
Modell < /p>
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace test.Models
{
public class ExportShipmentExBond
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? ExportShipmentExBondId { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string? ExporterMasterId { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[BsonIgnoreIfNull(false)]
public string? ExportShipmentExBondBENo { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[BsonIgnoreIfNull(false)]
public DateTime? ExportShipmentExBondBEDate { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[BsonIgnoreIfNull(false)]
public string? ExportShipmentExBondVessel { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[BsonIgnoreIfNull(false)]
public int? ExportShipmentExBondNoOfPackage { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[BsonRepresentation(BsonType.ObjectId)]
[BsonIgnoreIfNull(false)]
public string? ExportShipmentExBondNoOfPackageUnit { get; set; }

public string? ExportShipmentExbondStatus { get; set; } = "Y";
public DateTime ExportShipmentExBondCreatedDate { get; set; } = DateTime.UtcNow;
[BsonRepresentation(BsonType.ObjectId)]
public string? ExportShipmentExBondCreatedBy { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string? ExportShipmentExBondUpdatedBy { get; set; }
public DateTime ExportShipmentExBondUpdatedDate { get; set; } = DateTime.UtcNow;
}
}
< /code>
Problem: < /p>

Nur Updates wurden Felder vorgesehen. /> < /ol>
Frage: < /strong>
Wie kann ich nur die bereitgestellten Felder aktualisieren und mit dem C# -MongoDB -Treiber ausdrücklich ein Feld einstellen (einschließlich ObjektID -Zeichenfolgen), ohne dass andere Felder überschreiben?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post