public static bool Compare(BsonValue a, BsonValue b)
 {
     if (a.BsonType == BsonType.Document && b.BsonType == BsonType.Document)
     {
         return CompareDocuments((BsonDocument)a, (BsonDocument)b);
     }
     else if (a.BsonType == BsonType.Array && b.BsonType == BsonType.Array)
     {
         return CompareArrays((BsonArray)a, (BsonArray)b);
     }
     else if (a.BsonType == b.BsonType)
     {
         return a.Equals(b);
     }
     else if (IsNumber(a) && IsNumber(b))
     {
         return a.ToDouble() == b.ToDouble();
     }
     else if (CouldBeBoolean(a) && CouldBeBoolean(b))
     {
         return a.ToBoolean() == b.ToBoolean();
     }
     else
     {
         return false;
     }
 }
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
                case "documents":
                    _documents = ((BsonArray)value).OfType<BsonDocument>();
                    return true;
                case "ordered":
                    _options.IsOrdered = value.ToBoolean();
                    return true;
                case "writeConcern":
                    _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                    return true;
            }

            return false;
        }
Ejemplo n.º 3
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
                case "requests":
                    _requests = ParseRequests((BsonArray)value).ToList();
                    return true;
                case "ordered":
                    _options.IsOrdered = value.ToBoolean();
                    return true;
                case "writeConcern":
                    _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                    return true;
            }

            return false;
        }
Ejemplo n.º 4
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "requests":
                _requests = ParseRequests((BsonArray)value).ToList();
                return(true);

            case "ordered":
                _options.IsOrdered = value.ToBoolean();
                return(true);

            case "writeConcern":
                _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "replacement":
                _replacement = (BsonDocument)value;
                return(true);

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return(true);
            }

            return(false);
        }
Ejemplo n.º 6
0
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = new BsonDocumentFilterDefinition <BsonDocument>(value.AsBsonDocument);
                return;

            case "update":
                _update = new BsonDocumentUpdateDefinition <BsonDocument>(value.AsBsonDocument);
                return;

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return;
            }

            base.SetArgument(name, value);
        }
Ejemplo n.º 7
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "documents":
                _documents = ((BsonArray)value).OfType <BsonDocument>();
                return(true);

            case "ordered":
                _options.IsOrdered = value.ToBoolean();
                return(true);

            case "writeConcern":
                _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                return(true);
            }

            return(false);
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "bypassDocumentValidation":
                _options.BypassDocumentValidation = value.ToBoolean();
                return;

            case "document":
                _document = value.AsBsonDocument;
                return;

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

            base.SetArgument(name, value);
        }
Ejemplo n.º 9
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
                case "filter":
                    _filter = (BsonDocument)value;
                    return true;
                case "update":
                    _update = (BsonDocument)value;
                    return true;
                case "upsert":
                    _options.IsUpsert = value.ToBoolean();
                    return true;
                case "writeConcern":
                    _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                    return true;
            }

            return false;
        }
Ejemplo n.º 10
0
        private object GetDataTypeValueFromBsonValue(BsonValue value, DataType dt)
        {
            if (dt == DataType.DT_I8 | dt == DataType.DT_I4)
            {
                if (value.IsString)
                {
                    Int64 parsedInt = -1;
                    if (!Int64.TryParse(value.ToString(), out parsedInt))
                    {
                        bool pbCancel = true;
                        ComponentMetaData.FireError(0, "MongoDataSource", "Cannot parse string value to integer: " + value.ToString(), "", 0, out pbCancel);
                    }
                    return(parsedInt);
                }
                else
                {
                    return(value.ToInt64());
                }
            }
            else if (dt == DataType.DT_BOOL)
            {
                return(value.ToBoolean());
            }
            else if (dt == DataType.DT_R8 | dt == DataType.DT_R4)
            {
                return(value.ToDouble());
            }
            else if (dt == DataType.DT_DATE | dt == DataType.DT_DBTIMESTAMPOFFSET | dt == DataType.DT_DBTIMESTAMP)
            {
                return(DateTime.Parse(value.ToString()));
            }
            else
            {
                if (dt != DataType.DT_STR && !value.IsObjectId && !value.IsString && !value.IsBsonSymbol)
                {
                    ComponentMetaData.FireWarning(0, "MongoDataSource", "Converting " + value.BsonType + " to string, though datatype was " + dt, String.Empty, 0);
                }

                return(value.ToString());
            }
        }
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "replacement":
                _replacement = (BsonDocument)value;
                return(true);

            case "projection":
                _options.Projection = (BsonDocument)value;
                return(true);

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

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return(true);

            case "returnDocument":
                _options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), value.ToString());
                return(true);

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

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

            return(false);
        }
Ejemplo n.º 12
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);
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = new BsonDocumentFilterDefinition <BsonDocument>(value.AsBsonDocument);
                return;

            case "replacement":
                _replacement = value.AsBsonDocument;
                return;

            case "returnDocument":
                _options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), value.AsString);
                return;

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return;
            }

            base.SetArgument(name, value);
        }
Ejemplo n.º 14
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "update":
                _update = (BsonDocument)value;
                return(true);

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return(true);

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

            return(false);
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "options":
                SetArguments(value.AsBsonDocument);
                return;

            case "ordered":
                _options.IsOrdered = value.ToBoolean();
                return;

            case "requests":
                _requests = ParseWriteModels(value.AsBsonArray.Cast <BsonDocument>());
                return;

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

            base.SetArgument(name, value);
        }
Ejemplo n.º 16
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "update":
                _update = (BsonDocument)value;
                return(true);

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return(true);

            case "writeConcern":
                _writeConcern = WriteConcern.FromBsonDocument((BsonDocument)value);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 17
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "pipeline":
                _stages = ((BsonArray)value).Cast <BsonDocument>().ToList();
                return(true);

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

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

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

            return(false);
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = new BsonDocumentFilterDefinition <BsonDocument>(value.AsBsonDocument);
                return;

            case "replacement":
                _replacement = value.AsBsonDocument;
                return;

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

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return;
            }

            base.SetArgument(name, value);
        }
Ejemplo n.º 19
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "update":
                _update = value;
                return(true);

            case "upsert":
                _options.IsUpsert = value.ToBoolean();
                return(true);

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

            case "arrayFilters":
                var arrayFilters = new List <ArrayFilterDefinition>();
                foreach (BsonDocument arrayFilterDocument in value.AsBsonArray)
                {
                    var arrayFilter = new BsonDocumentArrayFilterDefinition <BsonDocument>(arrayFilterDocument);
                    arrayFilters.Add(arrayFilter);
                }
                _options.ArrayFilters = arrayFilters;
                return(true);

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

            return(false);
        }
Ejemplo n.º 20
0
        private object GetDataTypeValueFromBsonValue(BsonValue value, DataType dt)
        {
            if (dt == DataType.DT_I8 | dt == DataType.DT_I4)
            {
                if (value.IsString)
                {
                    Int64 parsedInt = -1;
                    if (!Int64.TryParse(value.ToString(), out parsedInt))
                    {
                        bool pbCancel = true;
                        ComponentMetaData.FireError(0, "MongoDataSource", "Cannot parse string value to integer: " + value.ToString(), "", 0, out pbCancel);
                    }
                    return parsedInt;
                }
                else
                {
                    return value.ToInt64();
                }
            }
            else if (dt == DataType.DT_BOOL)
            {
                return value.ToBoolean();
            }
            else if (dt == DataType.DT_R8 | dt == DataType.DT_R4)
            {
                return value.ToDouble();
            }
            else if (dt == DataType.DT_DATE | dt == DataType.DT_DBTIMESTAMPOFFSET | dt == DataType.DT_DBTIMESTAMP)
            {
                return DateTime.Parse(value.ToString());
            }
            else
            {
                if (dt != DataType.DT_STR && !value.IsObjectId && !value.IsString && !value.IsBsonSymbol)
                    ComponentMetaData.FireWarning(0, "MongoDataSource", "Converting " + value.BsonType + " to string, though datatype was " + dt, String.Empty, 0);

                return value.ToString();
            }
        }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
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);
        }
 private void AssertBoolean(bool?value, BsonValue expectedValue)
 {
     value.Should().Be(expectedValue.IsBsonNull ? (bool?)null : expectedValue.ToBoolean());
 }
Ejemplo n.º 24
0
            private object Bson2Object(BsonValue bd)
            {
                object rtn = null;

                if (bd.IsBsonNull)
                {
                    rtn = null;
                }
                else if (bd.IsBsonDateTime)
                {
                    //mongo存储的实际均为UTC标准时间,系统在运行时都是本地化的时间
                    rtn = bd.ToLocalTime();
                }
                else if (bd.IsBsonJavaScript)
                {
                    rtn = bd.AsBsonJavaScript.ToString();
                }
                else if (bd.IsBsonArray)
                {
                    var arr  = bd.AsBsonArray;
                    var list = new List <object>();
                    foreach (var item in arr)
                    {
                        list.Add(Bson2Object(item));
                    }

                    rtn = list.ToArray();
                }
                else if (bd.IsBsonDocument)
                {
                    rtn = Bson2Dynamic(bd.AsBsonDocument);
                }
                else if (bd.IsObjectId)
                {
                    rtn = bd.AsObjectId.Pid;
                }
                else if (bd.IsInt32)
                {
                    rtn = bd.AsInt32;
                }
                else if (bd.IsInt64)
                {
                    rtn = bd.AsInt64;
                }
                else if (bd.IsDouble)
                {
                    rtn = bd.AsDouble;
                }
                else if (bd.IsNumeric)
                {
                    rtn = decimal.Parse(bd.ToString());
                }
                else if (bd.IsBoolean)
                {
                    rtn = bd.ToBoolean();
                }
                else
                {
                    rtn = bd.AsString;
                }

                return(rtn);
            }