public ActionResult AddUpdateSave(ModUApiManager mod, FormCollection form) { ModUApiManager model = new ModUApiManager(); string status_name = CUtil.CStr(form["status_name"]); MongoCollection <ModUApiManager> mg = mongoh.GetCollection <ModUApiManager>(); var eq = Query.EQ("_id", new BsonString(mod._id)); if (status_name == "1") { model = mg.Find(eq).FirstOrDefault(); if (model != null) { return(Content(Util.JsUrlTo(Url.Action("Index"), "新增失败,香信号已存在"))); } model = new ModUApiManager(); model._id = mod._id.ToUpper(); model.is_disabled = mod.is_disabled; model.user_name = mod.user_name; model.last_login_time = DateTime.Now; mg.Insert(model); return(Content(Util.JsUrlTo(Url.Action("Index"), "新增成功"))); } model = mg.Find(eq).FirstOrDefault(); if (model == null) { return(Content(Util.JsUrlTo(Url.Action("Index"), "修改失败,数据不存在"))); } model.is_disabled = mod.is_disabled; model.user_name = mod.user_name; BsonDocument bd = BsonExtensionMethods.ToBsonDocument(model); mg.Update(Query.EQ("_id", new BsonString(mod._id)), new UpdateDocument(bd)); return(Content(Util.JsUrlTo(Url.Action("Index"), "修改成功"))); }
public bool UpdateUser(User user) { BsonDocument bd = new BsonDocument { { "$set", BsonExtensionMethods.ToBsonDocument(user) } }; UpdateResult ur = paySysDB.GetCollection <User>(Key.user.ToString()).UpdateOne(item => item.userId == user.userId, new UpdateDocument(bd)); return(ur.ModifiedCount == 1); }
public void Update <T>(T t, string strId) { MongoCollection <T> col = GetMongoCollection <T>(); BsonDocument bd = BsonExtensionMethods.ToBsonDocument(t); IMongoQuery query = Query.EQ("_id", new ObjectId(strId)); col.Update(query, new UpdateDocument(bd)); }
/// <summary> /// 修改 /// </summary> /// <param name="model"></param> /// <returns></returns> public long Update(T model) { if ((model as AbstractMongoModel).id_string != null) { (model as AbstractMongoModel).id = new ObjectId((model as AbstractMongoModel).id_string); } BsonDocument doc = BsonExtensionMethods.ToBsonDocument(model); WriteConcernResult res = this.collection.Update(Query.EQ("_id", (model as AbstractMongoModel).id), new UpdateDocument(doc)); return(res.DocumentsAffected); }
/// <summary> /// 更新数据 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <param name="dictUpdate">更新字段</param> public static void Update <T>(string collectionName, IMongoQuery query, T t) where T : EntityBase { var db = GetDatabase(); if (db != null) { var collection = db.GetCollection(collectionName); MongoCollection <T> col = db.GetCollection <T>(collectionName); BsonDocument bd = BsonExtensionMethods.ToBsonDocument(t); col.Update(query, new UpdateDocument(bd)); } }
public bool UpdateOrInsertQRCode(QRCode qrCode) { BsonDocument bd = new BsonDocument { { "$set", BsonExtensionMethods.ToBsonDocument(qrCode) } }; UpdateResult ur = paySysDB.GetCollection <QRCode>(Key.qrCode.ToString()).UpdateOne(item => item.name == qrCode.name && item.storageName == qrCode.storageName, new UpdateDocument(bd), new UpdateOptions() { IsUpsert = true }); return(ur.ModifiedCount == 1); }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Update(Securety model) { try { var query = Query.EQ("_id", model.Id); BsonDocument bd = BsonExtensionMethods.ToBsonDocument(model); WriteConcernResult wcr = mc.Update(query, new UpdateDocument(bd)); return(!wcr.HasLastErrorMessage); } catch (Exception ex) { return(false); } }
public int Update(T instance) { int resultCode = 0; Execute(delegate(MongoCollection <T> mongoCollection) { Func <T, string> _getId = new Func <T, string>(GetObjectId); QueryDocument query = new QueryDocument(new BsonElement("_id", BsonValue.Create(_getId(instance)))); SafeModeResult result = mongoCollection.Update(query, new UpdateDocument(BsonExtensionMethods.ToBsonDocument <T>(instance))); resultCode = 1; }); return(resultCode); }
/// <summary> /// 更新一条数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Update(T model, String Id) { try { var query = Query.EQ("_id", Id); BsonDocument bd = BsonExtensionMethods.ToBsonDocument(model); WriteConcernResult wcr = mc.Update(query, new UpdateDocument(bd)); return !wcr.HasLastErrorMessage; } catch (Exception ex) { return false; } }
public async Task <UpdateDefinition <TEntity> > CreateUpdateDefinitionAsync(TEntity updateEntity, bool isUpsert = false) { UpdateDefinition <TEntity> updater; BsonDocument bsDoc = BsonExtensionMethods.ToBsonDocument(updateEntity); bsDoc.Remove("_id"); updater = new BsonDocument("$set", bsDoc); if (isUpsert && updateEntity is IAutoInc) { long id = await GetIncID(); updater = UpdateDefinitionExtensions.SetOnInsert(updater, "_id", id); } return(updater); }
/// <summary> /// Update (根据条件修改数据) /// </summary> /// <param name="func">条件表达式</param> /// <param name="entity">修改对象</param> /// <returns>bool</returns> public bool Update(Expression <Func <T, bool> > func, T entity) { try { bool result = false; BsonDocument bsonDocument = BsonExtensionMethods.ToBsonDocument(entity); var collection = GetMongodbDataBase().GetCollection <T>(collectionName); var update = collection.UpdateOne(func, bsonDocument, new UpdateOptions() { IsUpsert = true }); result = update != null && update.ModifiedCount > 0; return(result); } catch (Exception ex) { throw new Exception(ex.Message); } }
// Input supposed to be not null static BsonDocument ToBsonDocumentFromProperties(BsonDocument source, PSObject value, ScriptBlock convert, IList <Selector> properties, int depth) { IncSerializationDepth(ref depth); var type = value.BaseObject.GetType(); if (type.IsPrimitive || type == typeof(string)) { throw new InvalidOperationException(Res.CannotConvert2(type, nameof(BsonDocument))); } // propertied omitted (null) of all (0)? if (properties == null || properties.Count == 0) { // if properties omitted (null) and the top (1) native object is not custom if (properties == null && depth == 1 && (!(value.BaseObject is PSCustomObject)) && TypeIsDriverSerialized(type)) { try { // serialize the top level native object var document = BsonExtensionMethods.ToBsonDocument(value.BaseObject, type); // return the result if (source == null) { return(document); } // add to the provided document source.AddRange(document.Elements); return(source); } catch (SystemException exn) { throw new InvalidOperationException(Res.CannotConvert3(type, nameof(BsonDocument), exn.Message), exn); } } else { // convert all properties to the source or new document var document = source ?? new BsonDocument(); foreach (var pi in value.Properties) { try { document.Add(pi.Name, ToBsonValue(pi.Value, convert, depth)); } catch (GetValueException) // .Value may throw, e.g. ExitCode in Process { document.Add(pi.Name, BsonNull.Value); } catch (SystemException exn) { if (depth == 1) { throw new InvalidOperationException(Res.CannotConvert3(type, nameof(BsonDocument), exn.Message), exn); } else { throw; } } } return(document); } } else { // existing or new document var document = source ?? new BsonDocument(); foreach (var selector in properties) { if (selector.PropertyName != null) { var pi = value.Properties[selector.PropertyName]; if (pi != null) { try { document.Add(selector.DocumentName, ToBsonValue(pi.Value, convert, depth)); } catch (GetValueException) // .Value may throw, e.g. ExitCode in Process { document.Add(selector.DocumentName, BsonNull.Value); } } } else { document.Add(selector.DocumentName, ToBsonValue(selector.GetValue(value), convert, depth)); } } return(document); } }
static BsonValue ToBsonValue(object value, ScriptBlock convert, int depth) { IncSerializationDepth(ref depth); if (value == null) { return(BsonNull.Value); } value = BaseObject(value, out PSObject custom); // case: custom if (custom != null) { return(ToBsonDocumentFromProperties(null, custom, convert, null, depth)); } // case: BsonValue if (value is BsonValue bson) { return(bson); } // case: string if (value is string text) { return(new BsonString(text)); } // case: document if (value is IConvertibleToBsonDocument cd) { return(cd.ToBsonDocument()); } // case: dictionary if (value is IDictionary dictionary) { return(ToBsonDocumentFromDictionary(null, dictionary, convert, null, depth)); } // case: bytes or collection if (value is IEnumerable en) { //_191108_183844 if (en is byte[] bytes) { return(new BsonBinaryData(bytes)); } var array = new BsonArray(); foreach (var it in en) { array.Add(ToBsonValue(it, convert, depth)); } return(array); } // try to map BsonValue if (BsonTypeMapper.TryMapToBsonValue(value, out BsonValue bson2)) { return(bson2); } // try to serialize class var type = value.GetType(); if (TypeIsDriverSerialized(type)) { return(BsonExtensionMethods.ToBsonDocument(value, type)); } // no converter? die if (convert == null) { //! use this type throw new ArgumentException(Res.CannotConvert2(type, nameof(BsonValue))); } try { value = DocumentInput.ConvertValue(convert, value); } catch (RuntimeException re) { //! use this type throw new ArgumentException($"Converter script was called for '{type}' and failed with '{re.Message}'.", re); } // do not pass converter twice return(ToBsonValue(value, null, depth)); }
public ActionResult AuditOneMessageSave(FormCollection form) { string id = form["_id"] ?? ""; string Audit = form["Audit"] ?? ""; string remark = form["remark"] ?? ""; if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(Audit)) { return(Content(Util.JsUrlTo(Url.Action("Index"), "数据错误"))); } MongoCollection <ModUApiPowerAplictionFind> uapiaplict = mongoh.GetCollection <ModUApiPowerAplictionFind>(); ModUApiPowerAplictionFind resultaplict = uapiaplict.Find(Query.EQ("_id", new ObjectId(id))).FirstOrDefault(); if (resultaplict == null) { return(Content(Util.JsUrlTo(Url.Action("Index"), "数据已删除或不存在"))); } if (!resultaplict.status.Contains("待审核")) { return(Content(Util.JsUrlTo(Url.Action("Index"), "申请信息已审核,请勿再次提交"))); } #region 查找出需要的接口 string needinter = ""; //首先查询这条记录是不是第一次申请,如果没有原有接口,那么就直接新增接口,如果有原有接口,那么就要在原有接口上移除需要的接口再加入新增的接口 if (resultaplict.interfaces != null && resultaplict.interfaces.Length > 0 && !string.IsNullOrEmpty(resultaplict.interfaces[0])) { #region 先得出所有需要移除的接口 string removie = ""; if (resultaplict.remove_interfaces != null && resultaplict.remove_interfaces.Length > 0 && !string.IsNullOrEmpty(resultaplict.remove_interfaces[0])) { foreach (var item1 in resultaplict.remove_interfaces) { removie += item1 + ","; } removie = removie.TrimEnd(','); } List <string> str = resultaplict.interfaces.ToList(); List <string> str_ = resultaplict.interfaces.ToList(); foreach (var item in str) { if (removie.Contains(item)) { str_.Remove(item); } } foreach (var item in str_) { needinter += item + ","; } #endregion if (resultaplict.add_interfaces != null && resultaplict.add_interfaces.Length > 0 && !string.IsNullOrEmpty(resultaplict.add_interfaces[0])) { foreach (var item1 in resultaplict.add_interfaces) { needinter += item1 + ","; } } } else { if (resultaplict.add_interfaces != null && resultaplict.add_interfaces.Length > 0 && !string.IsNullOrEmpty(resultaplict.add_interfaces[0])) { foreach (var item1 in resultaplict.add_interfaces) { needinter += item1 + ","; } } } needinter = needinter.TrimEnd(','); string[] Arrayneedinter = needinter.Split(','); #endregion #region 查找出需要的栏位 string needfields = ""; //首先查询这条记录是不是第一次申请,如何没有原有接口,那么就直接新增接口,如果有原有接口,那么就要在原有接口上移除需要的接口再加入新增的接口 if (resultaplict.fields != null && resultaplict.fields.Length > 0 && !string.IsNullOrEmpty(resultaplict.fields[0])) { #region 先得出所有需要移除的栏位 string removie = ""; if (resultaplict.remove_fields != null && resultaplict.remove_fields.Length > 0 && !string.IsNullOrEmpty(resultaplict.remove_fields[0])) { foreach (var item1 in resultaplict.remove_fields) { removie += item1 + ","; } } List <string> str = resultaplict.fields.ToList(); List <string> str_ = resultaplict.fields.ToList(); foreach (var item in str) { if (removie.Contains(item)) { str_.Remove(item); } } foreach (var item in str_) { needfields += item + ","; } #endregion if (resultaplict.add_fields != null && resultaplict.add_fields.Length > 0 && !string.IsNullOrEmpty(resultaplict.add_fields[0])) { foreach (var item1 in resultaplict.add_fields) { needfields += item1 + ","; } } } else { if (resultaplict.add_fields != null && resultaplict.add_fields.Length > 0 && !string.IsNullOrEmpty(resultaplict.add_fields[0])) { foreach (var item1 in resultaplict.add_fields) { needfields += item1 + ","; } } } needfields = needfields.TrimEnd(','); string[] Arrayneedfields = needfields.Split(','); #endregion //通过 if (Audit.Contains("1")) { //添加用户权限数据 var collection = mongoh.GetCollection <ModUApiPowerList>(); if (collection.Count(Query.EQ("_id", new BsonString(resultaplict.serviceno_uid))) == 0) { ModUApiPowerList power = new ModUApiPowerList { _id = resultaplict.serviceno_uid.ToUpper(), bg_name = resultaplict.bg_name, bu_name = resultaplict.bu_name, dept_name = resultaplict.dept_name, civetno = resultaplict.civetno, user_name = resultaplict.user_name, cost_code = resultaplict.cost_code, start_date = DateTime.Today, end_date = new DateTime(2020, 12, 31), fields = Arrayneedfields, interfaces = Arrayneedinter, ext_phone = resultaplict.ext_phone, mobile = resultaplict.mobile, limit_monthly_count = resultaplict.add_day, open_power = true, private_key_des = "" }; collection.Insert(power); } else { MongoCollection <ModUApiPowerList> uapipower = mongoh.GetCollection <ModUApiPowerList>(); ModUApiPowerList modpower = uapipower.Find(Query.EQ("_id", new BsonString(resultaplict.serviceno_uid))).FirstOrDefault(); if (modpower == null) { return(Content(Util.JsUrlTo(Url.Action("Index"), "数据不存在"))); } modpower.fields = Arrayneedfields; modpower.interfaces = Arrayneedinter; modpower.bg_name = resultaplict.bg_name; modpower.bu_name = resultaplict.bu_name; modpower.dept_name = resultaplict.dept_name; modpower.civetno = resultaplict.civetno; modpower.user_name = resultaplict.user_name; modpower.cost_code = resultaplict.cost_code; modpower.ext_phone = resultaplict.ext_phone; modpower.mobile = resultaplict.mobile; modpower.limit_monthly_count = resultaplict.add_day; BsonDocument bd = BsonExtensionMethods.ToBsonDocument(modpower); //修改将需要的接口和栏位保存到权限表中去 uapipower.Update(Query.EQ("_id", new BsonString(resultaplict.serviceno_uid)), new UpdateDocument(bd)); } uapiaplict.Update(Query.EQ("_id", new ObjectId(id)), Update.Set("status", "审核通过").Set("audit_time", DateTime.Now).Set("audit_civet_no", UserInfo.BaseInfo.civetno).Set("apply_reason", remark)); } else { if (Audit.Contains("2")) { uapiaplict.Update(Query.EQ("_id", new ObjectId(id)), Update.Set("status", "审核驳回").Set("audit_time", DateTime.Now).Set("audit_civet_no", UserInfo.BaseInfo.civetno).Set("apply_reason", remark)); } } return(Content(Util.JsUrlTo(Url.Action("Index")))); }