// 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); }
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); }
// 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); }
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); }
// 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); }
static bool Type(BsonDocument document, string name, BsonType type) { foreach (var data in document.GetNestedValues(name)) { if (data.BsonType == type) { return(true); } } return(false); }
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); }
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); }
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); }
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); }
// 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; }
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; }
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; }
// 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; }
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; }
// 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; }
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; }
static bool Type(BsonDocument document, string name, BsonType type) { foreach (var data in document.GetNestedValues(name)) if (data.BsonType == type) return true; return false; }