예제 #1
0
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return;

            case "filter":
                _filter = new BsonDocumentFilterDefinition <BsonDocument>(value.AsBsonDocument);
                return;

            case "limit":
                _options.Limit = value.ToInt32();
                return;

            case "session":
                _session = (IClientSessionHandle)_objectMap[value.AsString];
                return;

            case "sort":
                _options.Sort = (SortDefinition <BsonDocument>)value;
                return;
            }

            base.SetArgument(name, value);
        }
예제 #2
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "allowDiskUse":
                _options.AllowDiskUse = value.ToBoolean();
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "collation":
                _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
                return(true);
            }

            return(false);
        }
예제 #3
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
                case "filter":
                    _filter = (BsonDocument)value;
                    return true;
                case "sort":
                    _options.Sort = value.ToBsonDocument();
                    return true;
                case "limit":
                    _options.Limit = value.ToInt32();
                    return true;
                case "skip":
                    _options.Skip = value.ToInt32();
                    return true;
                case "batchSize":
                    _options.BatchSize = value.ToInt32();
                    return true;
                case "modifiers":
                    _options.Modifiers = (BsonDocument)value;
                    return true;
            }

            return false;
        }
예제 #4
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "modifiers":
                _options.Modifiers = (BsonDocument)value;
                return(true);
            }

            return(false);
        }
        // private methods
        private void AssertResultAspect(string name, BsonValue expectedValue)
        {
            switch (name)
            {
            case "matchedCount":
                _result.MatchedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "modifiedCount":
                _result.ModifiedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "upsertedCount":
                var upsertedCount = _result.UpsertedId == null ? 0 : 1;
                upsertedCount.Should().Be(expectedValue.ToInt32());
                break;

            case "upsertedId":
                _result.UpsertedId.Should().Be(expectedValue);
                break;

            default:
                throw new FormatException($"Invalid UpdateMany result aspect: {name}.");
            }
        }
        // private methods
        private void AssertResultAspect(string name, BsonValue expectedValue)
        {
            switch (name)
            {
            case "deletedCount":
                _result.DeletedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "insertedIds":
                _result.InsertedCount.Should().Be(expectedValue.AsBsonDocument.ElementCount);
                break;

            case "insertedCount":
                _result.InsertedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "matchedCount":
                _result.MatchedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "modifiedCount":
                _result.ModifiedCount.Should().Be(expectedValue.ToInt64());
                break;

            case "upsertedCount":
                _result.Upserts.Count.Should().Be(expectedValue.ToInt32());
                break;

            case "upsertedIds":
                _result.Upserts.Select(u => u.Id).Should().Equal(expectedValue.AsBsonDocument.Values);
                break;

            default: throw new FormatException($"Invalid BulkWriteResult aspect name: {name}.");
            }
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "timeoutMS":
                _timeout = TimeSpan.FromMilliseconds(value.ToInt32());
                return;
            }

            base.SetArgument(name, value);
        }
예제 #8
0
        static Expression PopExpression(Expression that, Expression field, BsonValue value)
        {
            int pop = value.IsNumeric ? value.ToInt32() : 0;

            if (pop != 1 && pop != -1)
            {
                throw new MongoException(string.Format(null, @"Value ""{0}"" must be 1 or -1.", value));
            }

            return(Expression.Call(that, GetMethod("Pop"), Data, field, Expression.Constant(pop, typeof(int))));
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return;

            case "pipeline":
                _pipeline = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(value.AsBsonArray.Cast <BsonDocument>());
                return;

            case "session":
                _session = (IClientSessionHandle)_objectMap[value.AsString];
                return;

            case "maxTimeMS":
                _options.MaxTime = TimeSpan.FromMilliseconds(value.ToInt32());
                return;
            }

            base.SetArgument(name, value);
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "count":
                _count = value.ToInt32();
                return;

            case "event":
                _event = value.ToString();
                return;
            }

            base.SetArgument(name, value);
        }
예제 #11
0
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return;

            case "filter":
                _filter = new BsonDocumentFilterDefinition <BsonDocument>(value.AsBsonDocument);
                return;
            }

            base.SetArgument(name, value);
        }
예제 #12
0
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return;

            case "pipeline":
                _pipeline = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(value.AsBsonArray.Cast <BsonDocument>());
                return;
            }

            base.SetArgument(name, value);
        }
예제 #13
0
        private static object Convert(BsonValue value, Type type)
        {
            switch (type.Name)
            {
            case "String":
                return(value.ToString());

            case "Int32":
                return(value.ToInt32());

            case "Int64":
                return(value.ToInt64());

            case "Decimal":
                return((decimal)value.ToDouble());

            case "Double":
                return(value.ToDouble());

            case "DateTime":
                return(value.ToLocalTime());

            case "Boolean":
                return(value.ToBoolean());

            case "Nullable`1":
                var nullType = type.GetProperty("Value").PropertyType;
                switch (nullType.Name)
                {
                case "DateTime":
                    return(value.ToNullableLocalTime());

                default:
                    return(Convert(value, nullType));
                }
            }
            return(value);
        }
예제 #14
0
        static Expression TypeExpression(Expression field, BsonValue args)
        {
            BsonType type;

            if (args.IsString)
            {
                switch (args.ToString())
                {
                case "double": type = BsonType.Double; break;

                case "string": type = BsonType.String; break;

                case "object": type = BsonType.Document; break;

                case "array": type = BsonType.Array; break;

                case "binData": type = BsonType.Binary; break;

                case "undefined": type = BsonType.Undefined; break;

                case "objectId": type = BsonType.ObjectId; break;

                case "bool": type = BsonType.Boolean; break;

                case "date": type = BsonType.DateTime; break;

                case "null": type = BsonType.Null; break;

                case "regex": type = BsonType.RegularExpression; break;

                case "dbPointer": type = (BsonType)12; break;

                case "javascript": type = BsonType.JavaScript; break;

                case "symbol": type = BsonType.Symbol; break;

                case "javascriptWithScope": type = BsonType.JavaScriptWithScope; break;

                case "int": type = BsonType.Int32; break;

                case "timestamp": type = BsonType.Timestamp; break;

                case "long": type = BsonType.Int64; break;

                case "minKey": type = BsonType.MinKey; break;

                case "maxKey": type = BsonType.MaxKey; break;

                case "number":
                    return(Expression.Call(GetMethod("TypeNumber"), Data, field));

                default:
                    throw new ArgumentException("Unknown string alias of $type argument.");
                }
            }
            else if (args.IsNumeric)
            {
                type = (BsonType)args.ToInt32();
            }
            else
            {
                throw new ArgumentException("$type argument must be number or string.");
            }

            return(Expression.Call(GetMethod("Type"), Data, field, Expression.Constant(type, typeof(BsonType))));
        }
예제 #15
0
        static Expression PushEachExpression(Expression that, Expression field, BsonDocument document)
        {
            int       position = -1;
            BsonArray each = null;
            BsonValue sort = null, slice = null;

            foreach (var e in document.Elements)
            {
                //TODO
                switch (e.Name)
                {
                case "$each":
                    if (e.Value.BsonType != BsonType.Array)
                    {
                        throw new ArgumentException("Push all/each value must be array.");
                    }
                    each = e.Value.AsBsonArray;
                    break;

                case "$sort":
                    sort = e.Value;
                    break;

                case "$slice":
                    if (!e.Value.IsNumeric)
                    {
                        throw new ArgumentException("$slice must be a numeric value.");
                    }
                    slice = e.Value;
                    break;

                case "$position":
                    if (!e.Value.IsNumeric)
                    {
                        throw new ArgumentException("$position must be a numeric value.");
                    }
                    position = e.Value.ToInt32();
                    if (position < 0)
                    {
                        throw new ArgumentException("$position must not be negative.");
                    }
                    break;

                default:
                    throw new ArgumentException(string.Format(null, "Unrecognized clause in $push: ({0}).", e.Name));
                }
            }

            if (sort != null)
            {
                switch (sort.BsonType)
                {
                case BsonType.Int32:
                    switch (sort.AsInt32)
                    {
                    case 1:
                        each = new BsonArray(each.OrderBy(x => x)); break;                                         //TODO comparer?

                    case -1:
                        each = new BsonArray(each.OrderByDescending(x => x)); break;                                         //TODO comparer?

                    default:
                        throw new ArgumentException("Numeric $sort value must be either 1 or -1.");
                    }
                    break;

                case BsonType.Document:
                    //TODO not needed in Mongo v2.6
                    foreach (var d1 in each)
                    {
                        if (d1.BsonType != BsonType.Document)
                        {
                            throw new ArgumentException("$sort requires $each to be an array of objects.");
                        }
                    }

                    each = new BsonArray(QueryCompiler.Query(each.Cast <BsonDocument>(), null, null, new SortByDocument(sort.AsBsonDocument), 0, 0));
                    break;

                default:
                    throw new ArgumentException("$sort value must be 1, -1, or a document.");
                }
            }

            if (slice != null)
            {
                each = each.Slice(0, slice.ToInt32());
            }

            return(Expression.Call(that, GetMethod("PushAll"), Data, field,
                                   Expression.Constant(each, typeof(BsonValue)), Expression.Constant(position, typeof(int))));
        }
예제 #16
0
        static Expression PopExpression(Expression that, Expression field, BsonValue value)
        {
            int pop = value.IsNumeric ? value.ToInt32() : 0;

            return(Expression.Call(that, GetMethod("Pop"), Data, field, Expression.Constant(pop, typeof(int))));
        }
예제 #17
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "comment":
                _options.Comment = value.AsString;
                return(true);

            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "modifiers":
#pragma warning disable 618
                _options.Modifiers = (BsonDocument)value;
#pragma warning restore 618
                return(true);

            case "hint":
                _options.Hint = value;
                return(true);

            case "max":
                _options.Max = value.ToBsonDocument();
                return(true);

            case "min":
                _options.Min = value.ToBsonDocument();
                return(true);

            case "maxTimeMS":
                _options.MaxTime = TimeSpan.FromMilliseconds(value.ToInt32());
                return(true);

            case "returnKey":
                _options.ReturnKey = value.ToBoolean();
                return(true);

            case "showRecordId":
                _options.ShowRecordId = value.ToBoolean();
                return(true);
            }

            return(false);
        }
예제 #18
0
        private MongoNode AddBsonValueNode(string key, BsonValue v, MongoNode parent)
        {
            MongoNode node = new MongoNode {
                Key = key
            };

            if (v.IsBsonNull)
            {
                node = AddTreeNode(key, null, parent);
            }
            else if (v.IsBoolean)
            {
                node = AddTreeNode(key, v.ToBoolean(), parent);
            }
            else if (v.IsInt32)
            {
                node = AddTreeNode(key, v.ToInt32(), parent);
            }
            else if (v.IsInt32 || v.IsInt64)
            {
                node = AddTreeNode(key, v.ToInt64(), parent);
            }
            else if (v.IsDouble)
            {
                node = AddTreeNode(key, v.ToDouble(), parent);
            }
            else if (v.IsDecimal128 || v.IsNumeric)
            {
                node = AddTreeNode(key, v.ToDecimal(), parent);
            }
            else if (v.IsObjectId)
            {
                node = AddTreeNode(key, v.AsObjectId, parent);
            }
            else if (v.IsString || v.IsGuid)
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }
            else if (v.IsValidDateTime || v.IsBsonDateTime)
            {
                node = AddTreeNode(key, v.ToLocalTime(), parent);
            }
            else if (v.IsString)
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }
            else if (v.IsValidDateTime)
            {
                node = AddTreeNode(key, v.ToLocalTime(), parent);
            }
            else if (v.IsBsonArray)
            {
                var array = v.AsBsonArray;
                node.Value     = $"[{array.Count}]";
                node.Type      = "Array";
                node.ImagePath = ResourcesBase + @"/Images/arr.png";
                var i = 0;
                foreach (var item in array)
                {
                    AddBsonValueNode($"[{i}]", item, node);
                    i++;
                }
            }
            else
            {
                node = AddTreeNode(key, v.ToString(), parent);
            }

            return(node);
        }
예제 #19
0
 static Expression PopExpression(Expression that, Expression field, BsonValue value)
 {
     int pop = value.IsNumeric ? value.ToInt32() : 0;
     return Expression.Call(that, GetMethod("Pop"), Data, field, Expression.Constant(pop, typeof(int)));
 }
예제 #20
0
        static Expression TypeExpression(Expression field, BsonValue args)
        {
            if (!args.IsNumeric)
                throw new ArgumentException("$type argument must be number.");

            return Expression.Call(GetMethod("Type"), Data, field, Expression.Constant((BsonType)args.ToInt32(), typeof(BsonType)));
        }