Exemplo n.º 1
0
        // If missing or not a number then false else numbers are converted to long.
        static bool Mod(BsonDocument document, string name, long divisor, long value)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                switch (data.BsonType)
                {
                case BsonType.Int64:
                    if (data.AsInt64 % divisor == value)
                    {
                        return(true);
                    }
                    break;

                case BsonType.Int32:
                    if ((long)data.AsInt32 % divisor == value)
                    {
                        return(true);
                    }
                    break;

                case BsonType.Double:
                    if ((long)data.AsDouble % divisor == value)
                    {
                        return(true);
                    }
                    break;
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        static bool In(BsonDocument document, string name, BsonArray value)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                {
                    if (value.FirstOrDefault(x => data.EqualsOrMatches(x)) != null)
                    {
                        return(true);
                    }
                    else
                    {
                        continue;
                    }
                }

                foreach (var it in data.AsBsonArray)
                {
                    if (value.FirstOrDefault(x => it.EqualsOrMatches(x)) != null)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        // If the field is missing or the size is empty then false.
        static bool All(BsonDocument document, string name, List <object> all)
        {
            if (all.Count == 0)
            {
                return(false);
            }

            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                {
                    foreach (var one in all)
                    {
                        if (!data.EqualsOrElemMatches(one))
                        {
                            goto next;
                        }
                    }
                    return(true);
                }

                foreach (var one in all)
                {
                    if (data.AsBsonArray.FirstOrDefault(x => x.EqualsOrElemMatches(one)) == null)
                    {
                        goto next;
                    }
                }
                return(true);

                next :;
            }

            return(false);
        }
Exemplo n.º 4
0
 static bool GT(BsonDocument document, string name, BsonValue value)
 {
     foreach (var data in document.GetNestedValues(name))
     {
         if (data.CompareTo(value) > 0)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 5
0
        // If missing or not a number then false.
        // If value is not a number then size 0 is used instead.
        // Why double: Mongo allows long and doubles and compares doubles with sizes literally.
        static bool Size(BsonDocument document, string name, double size)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType == BsonType.Array && data.AsBsonArray.Count == size)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        static bool Type(BsonDocument document, string name, BsonType type)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType == type)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 7
0
        static bool Matches(BsonDocument document, string name, Regex regex)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType == BsonType.String && regex.IsMatch(data.AsString))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 8
0
        static bool NE(BsonDocument document, string name, BsonValue value)
        {
            bool ok = false;

            foreach (var data in document.GetNestedValues(name))
            {
                ok = true;
                if (data.CompareTo(value) != 0)
                {
                    return(true);
                }
            }
            return(ok ? false : value.BsonType != BsonType.Null);
        }
Exemplo n.º 9
0
        static bool TypeNumber(BsonDocument document, string name)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                switch (data.BsonType)
                {
                case BsonType.Double: return(true);

                case BsonType.Int32: return(true);

                case BsonType.Int64: return(true);
                }
            }

            return(false);
        }
Exemplo n.º 10
0
        static bool ElemMatch(BsonDocument document, string name, Func <BsonDocument, bool> predicate)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                {
                    continue;
                }

                foreach (var doc in data.AsBsonArray)
                {
                    if (doc.BsonType == BsonType.Document && predicate(doc.AsBsonDocument))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 11
0
        // If the field is missing or the size is empty then false.
        static bool All(BsonDocument document, string name, List<object> all)
        {
            if (all.Count == 0)
                return false;

            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                {
                    foreach (var one in all)
                        if (!data.EqualsOrElemMatches(one))
                            goto next;
                    return true;
                }

                foreach (var one in all)
                    if (data.AsBsonArray.FirstOrDefault(x => x.EqualsOrElemMatches(one)) == null)
                        goto next;
                return true;

            next: ;
            }

            return false;
        }
Exemplo n.º 12
0
        static bool In(BsonDocument document, string name, BsonArray value)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                    if (value.FirstOrDefault(x => data.EqualsOrMatches(x)) != null)
                        return true;
                    else
                        continue;

                foreach (var it in data.AsBsonArray)
                    if (value.FirstOrDefault(x => it.EqualsOrMatches(x)) != null)
                        return true;
            }

            return false;
        }
Exemplo n.º 13
0
        static bool Matches(BsonDocument document, string name, Regex regex)
        {
            if (regex == null)
                return false;

            foreach (var data in document.GetNestedValues(name))
                if (data.BsonType == BsonType.String && regex.IsMatch(data.AsString))
                    return true;

            return false;
        }
Exemplo n.º 14
0
        // If missing or not a number then false else numbers are converted to long.
        static bool Mod(BsonDocument document, string name, long divisor, long value)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                switch (data.BsonType)
                {
                    case BsonType.Int64:
                        if (data.AsInt64 % divisor == value)
                            return true;
                        break;
                    case BsonType.Int32:
                        if ((long)data.AsInt32 % divisor == value)
                            return true;
                        break;
                    case BsonType.Double:
                        if ((long)data.AsDouble % divisor == value)
                            return true;
                        break;
                }
            }

            return false;
        }
Exemplo n.º 15
0
 static bool NE(BsonDocument document, string name, BsonValue value)
 {
     bool ok = false;
     foreach (var data in document.GetNestedValues(name))
     {
         ok = true;
         if (data.CompareTo(value) != 0)
             return true;
     }
     return ok ? false : value.BsonType != BsonType.Null;
 }
Exemplo n.º 16
0
        // If missing or not a number then false.
        // If value is not a number then size 0 is used instead.
        // Why double: Mongo allows long and doubles and compares doubles with sizes literally.
        static bool Size(BsonDocument document, string name, double size)
        {
            foreach (var data in document.GetNestedValues(name))
                if (data.BsonType == BsonType.Array && data.AsBsonArray.Count == size)
                    return true;

            return false;
        }
Exemplo n.º 17
0
        static bool ElemMatch(BsonDocument document, string name, Func<BsonDocument, bool> predicate)
        {
            foreach (var data in document.GetNestedValues(name))
            {
                if (data.BsonType != BsonType.Array)
                    continue;

                foreach (var doc in data.AsBsonArray)
                    if (doc.BsonType == BsonType.Document && predicate(doc.AsBsonDocument))
                        return true;
            }

            return false;
        }
Exemplo n.º 18
0
        static bool Type(BsonDocument document, string name, BsonType type)
        {
            foreach (var data in document.GetNestedValues(name))
                if (data.BsonType == type)
                    return true;

            return false;
        }