標籤:string var format mod property 資料 app ase lis
var queryDetail = new BsonDocument("cNo", doc.cNo);
queryDetail.AddRange(new BsonDocument("Details.dDate", doc.oRDate));
//日表數組中存在相同報表日期的資料
bool exist = tblDays.FindSync(queryDetail).Any();
if (exist)
{
#region 日表數組中存在相同報表日期的資料,則按欄位更新
List<WriteModel<BsonDocument>> requests = new List<WriteModel<BsonDocument>>();
requests.Add(new UpdateOneModel<BsonDocument>(_query, update) { IsUpsert = true });
var updateDetail = new BsonDocument();
foreach (var propert in properties)
{
var vals = propert.GetValue(detail);
if (propert.PropertyType.BaseType.Name.Equals("Enum"))
{
vals = Convert.ToInt64(vals);
}
var tmpUpdate = new BsonDocument() { { "$set", new BsonDocument() { { string.Format("Details.$.{0}", propert.Name), BsonDocumentWrapper.Create(vals) } } } };
requests.Add(new UpdateOneModel<BsonDocument>(queryDetail, tmpUpdate) { IsUpsert = true });
}
if (requests.Count > 0)
{
tblDays.BulkWrite(requests);
}
#endregion
}
else {
#region 日表數組中不存在相同報表日期的資料,則直接新增到數組
var updateSet = new BsonDocument() { { "$addToSet", new BsonDocument() { { "Details", BsonDocumentWrapper.Create(detail) } } } };
update.AddRange(updateSet);
tblDays.UpdateOne(_query, update, new UpdateOptions { IsUpsert = true });
#endregion
}
c# mongo 數組裡對象更新