/// <summary> /// /// <para>GetItem</para> /// /// <para>Gets an item from a table, if _ValuesToGet is null; will retrieve all.</para> /// /// <para>Check <seealso cref="IBDatabaseServiceInterface.GetItem"/> for detailed documentation</para> /// /// </summary> public bool GetItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, string[] _ValuesToGet, out JObject _Result, Action <string> _ErrorMessageAction = null) { _Result = null; var Table = GetTable(_Table); if (Table == null) { return(false); } try { var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString()); BsonDocument Document = Table.Find(Filter).FirstOrDefault(); if (Document != null) { _Result = BsonToJObject(Document); } return(true); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BDatabaseServiceMongoDB->GetItem: {ex.Message} \n {ex.StackTrace}"); } return(false); }
/// <summary> /// /// <para>UpdateItem</para> /// /// <para>Updates an item in a table</para> /// /// <para>Check <seealso cref="IBDatabaseServiceInterface.UpdateItem"/> for detailed documentation</para> /// /// </summary> public bool UpdateItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, JObject _UpdateItem, out JObject _ReturnItem, EBReturnItemBehaviour _ReturnItemBehaviour = EBReturnItemBehaviour.DoNotReturn, BDatabaseAttributeCondition _ConditionExpression = null, Action <string> _ErrorMessageAction = null) { _ReturnItem = null; var Table = GetTable(_Table); if (Table == null) { return(false); } try { var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString()); if (_ConditionExpression != null) { Filter = Builders <BsonDocument> .Filter.And(Filter, (_ConditionExpression as BDatabaseAttributeConditionMongo).Filter); } JObject NewObject = (JObject)_UpdateItem.DeepClone(); AddKeyToJson(NewObject, _KeyName, _KeyValue); BsonDocument Document = new BsonDocument { { "$set", JObjectToBson(NewObject) } }; //use $set for preventing to get element name is not valid exception. more info https://stackoverflow.com/a/35441075 if (_ReturnItemBehaviour == EBReturnItemBehaviour.DoNotReturn) { Table.UpdateOne(Filter, Document, new UpdateOptions() { IsUpsert = true }); return(true); } else { BsonDocument OldDocument = Table.FindOneAndUpdate(Filter, Document); if (Document != null) { _ReturnItem = JObject.Parse(Document.ToJson()); return(true); } } } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BDatabaseServiceMongoDB->UpdateItem: {ex.Message} : \n {ex.StackTrace}"); } return(false); }
/// <summary> /// /// <para>IncrementOrDecrementItemValue</para> /// /// <para>Updates an item in a table, if item does not exist, creates a new one with only increment/decrement value</para> /// /// <para>Check <seealso cref="IBDatabaseServiceInterface.IncrementOrDecrementItemValue"/> for detailed documentation</para> /// /// </summary> public bool IncrementOrDecrementItemValue(string _Table, string _KeyName, BPrimitiveType _KeyValue, out double _NewValue, string _ValueAttribute, double _IncrementOrDecrementBy, bool _bDecrement = false, Action <string> _ErrorMessageAction = null) { _NewValue = 0.0f; var Table = GetTable(_Table); if (Table == null) { return(false); } var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString()); UpdateDefinition <BsonDocument> Update = null; if (_bDecrement) { Update = Builders <BsonDocument> .Update.Inc(_ValueAttribute, -_IncrementOrDecrementBy); } else { Update = Builders <BsonDocument> .Update.Inc(_ValueAttribute, _IncrementOrDecrementBy); } try { BsonDocument Document = Table.FindOneAndUpdate(Filter, Update, new FindOneAndUpdateOptions <BsonDocument, BsonDocument>() { ReturnDocument = ReturnDocument.After }); _NewValue = Document.GetValue(_ValueAttribute).AsDouble; return(true); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"{ex.Message} : \n {ex.StackTrace}"); } return(false); }
/// <summary> /// /// <para>DeleteItem</para> /// /// <para>Deletes an item from a table</para> /// <para>Note: Whether _ReturnItemBehaviour set to All or Updated, returns All</para> /// /// <para>Check <seealso cref="IBDatabaseServiceInterface.DeleteItem"/> for detailed documentation</para> /// /// </summary> public bool DeleteItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, out JObject _ReturnItem, EBReturnItemBehaviour _ReturnItemBehaviour = EBReturnItemBehaviour.DoNotReturn, Action <string> _ErrorMessageAction = null) { _ReturnItem = null; var Table = GetTable(_Table); if (Table == null) { return(false); } var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString()); try { if (_ReturnItemBehaviour == EBReturnItemBehaviour.DoNotReturn) { Table.DeleteOne(Filter); _ReturnItem = null; return(true); } else { BsonDocument Document = Table.FindOneAndDelete(Filter); if (Document != null) { _ReturnItem = BsonToJObject(Document); } return(true); } } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BDatabaseServiceMongoDB->DeleteItem: {ex.Message} : \n {ex.StackTrace}"); } return(false); }
/// <summary> /// /// <para>RemoveElementsFromArrayItem</para> /// /// <para>Removes element from the array item</para> /// /// <para>Check <seealso cref="IBDatabaseServiceInterface.RemoveElementsFromArrayItem"/> for detailed documentation</para> /// /// </summary> public bool RemoveElementsFromArrayItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, string _ElementName, BPrimitiveType[] _ElementValueEntries, out JObject _ReturnItem, EBReturnItemBehaviour _ReturnItemBehaviour, Action <string> _ErrorMessageAction) { _ReturnItem = null; if (_ElementValueEntries == null || _ElementValueEntries.Length == 0) { _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: ElementValueEntries must contain values."); return(false); } var ExpectedType = _ElementValueEntries[0].Type; foreach (var _ElementValueEntry in _ElementValueEntries) { if (_ElementValueEntry.Type != ExpectedType) { _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: ElementValueEntries must contain elements with the same type."); return(false); } } if (_KeyValue == null) { _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: Key is null."); return(false); } var Table = GetTable(_Table); if (Table == null) { return(false); } var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString()); List <object> TempList = new List <object>(); foreach (var Element in _ElementValueEntries) { switch (Element.Type) { case EBPrimitiveTypeEnum.String: TempList.Add(Element.AsString); break; case EBPrimitiveTypeEnum.Integer: TempList.Add(Element.AsInteger); break; case EBPrimitiveTypeEnum.Double: TempList.Add(Element.AsDouble); break; case EBPrimitiveTypeEnum.ByteArray: TempList.Add(Element.AsByteArray); break; } } UpdateDefinition <BsonDocument> Update = Builders <BsonDocument> .Update.PullAll(_ElementName, TempList); try { if (_ReturnItemBehaviour == EBReturnItemBehaviour.DoNotReturn) { Table.UpdateOne(Filter, Update); return(true); } else { if (_ReturnItemBehaviour == EBReturnItemBehaviour.ReturnAllOld) { BsonDocument Document = Table.FindOneAndUpdate(Filter, Update, new FindOneAndUpdateOptions <BsonDocument, BsonDocument>() { ReturnDocument = ReturnDocument.Before }); _ReturnItem = JObject.Parse(Document.ToJson()); return(true); } else { BsonDocument Document = Table.FindOneAndUpdate(Filter, Update, new FindOneAndUpdateOptions <BsonDocument, BsonDocument>() { ReturnDocument = ReturnDocument.After }); _ReturnItem = JObject.Parse(Document.ToJson()); return(true); } } } catch (Exception ex) { _ErrorMessageAction?.Invoke($"{ex.Message} : \n {ex.StackTrace}"); return(false); } }