Beispiel #1
0
        private static void DoPush(BsonDocument doc, string name, BsonValue value)
        {
            var       arrayNode = BsonHelpers.GetDocumentValue(name, doc);
            var       isEach    = value.IsBsonDocument && value.AsBsonDocument.First().Name == "$each";
            BsonArray arrayValues;

            if (isEach)
            {
                arrayValues = value.AsBsonDocument.First().Value.AsBsonArray;
            }
            else
            {
                arrayValues = BsonArray.Create(new BsonValue[] { value });
            }

            if (arrayNode == BsonNull.Value)
            {
                BsonHelpers.SetDocumentValue(name, doc, arrayValues, isUnset: false);
                return;
            }

            if (!arrayNode.IsBsonArray)
            {
                throw new InMemoryDatabaseException($"Cannot push to element [{arrayNode.BsonType.ToString()}] '{name}', it is not an array");
            }

            var node = arrayNode.AsBsonArray;

            node.AddRange(arrayValues);
        }
Beispiel #2
0
        private static void DoMultiply(BsonDocument doc, string name, BsonValue value)
        {
            var       currentValue = BsonHelpers.GetDocumentValue(name, doc);
            BsonValue newValue;

            if (currentValue == BsonNull.Value)
            {
                newValue = CastTo(value.BsonType, 0);
            }
            else
            {
                newValue = BsonHelpers.Multiply(currentValue, value);
            }
            DoSet(doc, name, newValue);
        }
Beispiel #3
0
        private static void DoInc(BsonDocument doc, string name, BsonValue value)
        {
            var       currentValue = BsonHelpers.GetDocumentValue(name, doc);
            BsonValue newValue;

            if (currentValue == BsonNull.Value)
            {
                newValue = value;
            }
            else
            {
                newValue = BsonHelpers.Add(currentValue, value);
            }
            DoSet(doc, name, newValue);
        }
Beispiel #4
0
 private static BsonValue GetDocumentValue(string name, BsonDocument doc)
 => BsonHelpers.GetDocumentValue(name, doc);
Beispiel #5
0
 private static void DoSet(BsonDocument doc, string name, BsonValue value)
 {
     BsonHelpers.SetDocumentValue(name, doc, value, isUnset: false);
 }