Beispiel #1
0
        UpdateCompiler Mul(BsonDocument document, string name, BsonValue value)
        {
            var       r = document.EnsurePath(name);
            BsonValue old;

            if (r.Document != null)
            {
                if (r.Document.TryGetValue(r.Key, out old))
                {
                    if (!old.IsNumeric)
                    {
                        throw new MongoException(string.Format(null, @"Field ""{0}"" must be numeric.", name));
                    }
                }
                else
                {
                    old = 0;
                }

                r.Document[r.Key] = MyValue.Mul(old, value);
            }
            else if (!r.Array.InsertOutOfRange(r.Index, () => MyValue.Mul(0, value)))
            {
                old = r.Array[r.Index];
                if (!old.IsNumeric)
                {
                    throw new MongoException(string.Format(null, @"Item ""{0}"" must be numeric.", name));
                }

                r.Array[r.Index] = MyValue.Mul(old, value);
            }
            return(this);
        }
Beispiel #2
0
        static void Bitwise(BsonDocument document, string name, BsonValue value, int type)
        {
            var r = document.EnsurePath(name);

            BsonValue old;

            if (r.Array != null)
            {
                r.Array.InsertOutOfRange(r.Index, () => 0);

                old = r.Array[r.Index];
                if (old.BsonType != BsonType.Int32 && old.BsonType != BsonType.Int64)
                {
                    throw new MongoException(string.Format(null, @"Item ""{0}"" must be Int32 or Int64.", name));
                }

                switch (type)
                {
                case 0: r.Array[r.Index] = MyValue.BitwiseAnd(old, value); break;

                case 1: r.Array[r.Index] = MyValue.BitwiseOr(old, value); break;

                default: r.Array[r.Index] = MyValue.BitwiseXor(old, value); break;
                }
            }
            else if (r.Document.TryGetValue(r.Key, out old))
            {
                if (old.BsonType != BsonType.Int32 && old.BsonType != BsonType.Int64)
                {
                    throw new MongoException(string.Format(null, @"Field ""{0}"" must be Int32 or Int64.", name));
                }

                switch (type)
                {
                case 0: r.Document[r.Key] = MyValue.BitwiseAnd(old, value); break;

                case 1: r.Document[r.Key] = MyValue.BitwiseOr(old, value); break;

                default: r.Document[r.Key] = MyValue.BitwiseXor(old, value); break;
                }
            }
        }