private MongoDB.Bson.BsonDocument Aggrigate(MongoDB.Bson.BsonDocument _doc) { try { string destinationValue = ""; string[] sourceKeyArr = SourceKeys.Split(','); foreach (string sourceKey in sourceKeyArr) { if (_doc[sourceKey] != null) { if (this.SourceKeyTransformation.Key == sourceKey) { destinationValue += this.SourceKeyTransformation.Execute(_doc); } else { destinationValue += _doc[sourceKey] + Delimiter; } } } if (destinationValue.EndsWith(Delimiter)) { destinationValue = destinationValue.TrimEnd(Delimiter.ToCharArray()); } _doc.Add(DestinationKey, destinationValue); } catch (Exception exp) { Console.WriteLine(exp.Message); throw; } return(_doc); }
/// <summary> /// 将类转换为BsonDocument以放入MongoDB,类中每个字段都可转换为BsonValue /// </summary> /// <param name="obj">待转换的对象</param> /// <returns>转换后的BsonDocument对象</returns> public static MongoDB.Bson.BsonDocument ToMongoDocument(object obj) { MongoDB.Bson.BsonDocument document = new MongoDB.Bson.BsonDocument(); foreach (PropertyInfo property in obj.GetType().GetProperties()) { object propertyValue = property.GetValue(obj, null); if (propertyValue == null) { document.Add(property.Name, MongoDB.Bson.BsonNull.Value); } else { document.Add(property.Name, MongoDB.Bson.BsonValue.Create(propertyValue)); } } return(document); }
private MongoDB.Bson.BsonDocument Swap(MongoDB.Bson.BsonDocument _doc) { var keys = JsonConvert.DeserializeObject <List <SwapKeyValue> >(this.SwapKeyValues); foreach (SwapKeyValue skv in keys) { MongoDB.Bson.BsonElement v = new MongoDB.Bson.BsonElement(); if (_doc.TryGetElement(skv.Source, out v)) { _doc.Add(new MongoDB.Bson.BsonElement(skv.Destination, _doc[skv.Source])); _doc.Remove(skv.Source); } } return(_doc); }
public string UpdateMeter(Meter meter) { MongoDBHelper <Meter> mongo = new MongoDBHelper <Meter>(); var query = new QueryDocument(); query.Add("_id", meter._id); var update = new UpdateDocument(); update.Add("MeterID", meter.MeterID); update.Add("UserID", meter.UserID == null?"":meter.UserID); update.Add("Mac", meter.Mac); update.Add("MKeyVer", meter.MKeyVer); update.Add("Key", meter.Key); update.Add("MeterType", meter.MeterType); update.Add("TotalAmount", (double)meter.TotalAmount); update.Add("TotalTopUp", (double)meter.TotalTopUp); update.Add("SettlementType", meter.SettlementType); update.Add("SettlementDay", meter.SettlementDay); update.Add("SettlementMonth", meter.SettlementMonth); update.Add("ValveState", meter.ValveState); update.Add("MeterState", meter.MeterState); update.Add("PriceCheck", meter.PriceCheck); update.Add("LastTopUpSer", meter.LastTopUpSer); update.Add("LastJiaoShiDate", meter.LastJiaoShiDate == null ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : meter.LastJiaoShiDate); update.Add("IsUsedLadder", meter.IsUsedLadder); update.Add("Ladder", meter.Ladder); update.Add("Price1", (double)meter.Price1); update.Add("Price2", (double)meter.Price2); update.Add("Price3", (double)meter.Price3); update.Add("Price4", (double)meter.Price4); update.Add("Price5", (double)meter.Price5); update.Add("Gas1", (double)meter.Gas1); update.Add("Gas2", (double)meter.Gas2); update.Add("Gas3", (double)meter.Gas3); update.Add("Gas4", (double)meter.Gas4); update.Add("LastSettlementAmount", (double)meter.LastSettlementAmount); update.Add("LastTotal", (double)meter.LastTotal); //update.Add("MeterType", meter.MeterType); update.Add("NextSettlementPointGas", (double)meter.NextSettlementPointGas);//下一个结算点气量 update.Add("CurrentLadder", meter.CurrentLadder); update.Add("CurrentPrice", (double)meter.CurrentPrice); update.Add("LastGasPoint", (double)meter.LastGasPoint); update.Add("CurrentBalance", (double)meter.CurrentBalance); update.Add("LJMoney", (double)meter.LJMoney); update.Add("SettlementDateTime", meter.SettlementDateTime == null ? "":meter.SettlementDateTime); update.Add("IsDianHuo", meter.IsDianHuo); update.Add("IsPricing", meter.IsPricing); update.Add("BillID", meter.BillID); update.Add("TiaoJiaPointGas", (double)meter.TiaoJiaPointGas); if (meter.PricingPlan != null) { MongoDB.Bson.BsonDocument b = new MongoDB.Bson.BsonDocument(); b.Add("IsUsedLadder", meter.PricingPlan.IsUsedLadder); b.Add("Ladder", meter.PricingPlan.Ladder); b.Add("Price1", (double)meter.PricingPlan.Price1); b.Add("Gas1", (double)meter.PricingPlan.Gas1); b.Add("Price2", (double)meter.PricingPlan.Price2); b.Add("Gas2", (double)meter.PricingPlan.Gas2); b.Add("Price3", (double)meter.PricingPlan.Price3); b.Add("Gas3", (double)meter.PricingPlan.Gas3); b.Add("Price4", (double)meter.PricingPlan.Price4); b.Add("Gas4", (double)meter.PricingPlan.Gas4); b.Add("Price5", (double)meter.PricingPlan.Price5); b.Add("MeterType", meter.PricingPlan.MeterType); b.Add("UseDate", meter.PricingPlan.UseDate); update.Add("PricingPlan", b); } mongo.Update(CollectionNameDefine.MeterCollectionName, query, update); return(""); }