コード例 #1
0
        private bool Match(DynamicObject dynobj, string name)
        {
            var value = dynobj.GetMember(name);

            foreach (var key in this.values.Keys)
            {
                if (key == "$exists")
                {
                    if (!dynobj.Exists(name).Equals(this.GetMember(key)))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$lt")
                {
                    if (!(((IComparable)value).CompareTo(this.EvaluateMember(key)) < 0))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$lte")
                {
                    if (!(((IComparable)value).CompareTo(this.EvaluateMember(key)) <= 0))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$gt")
                {
                    if (!(((IComparable)value).CompareTo(this.EvaluateMember(key)) > 0))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$gte")
                {
                    if (!(((IComparable)value).CompareTo(this.EvaluateMember(key)) >= 0))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$ne")
                {
                    if (value.Equals(this.EvaluateMember(key)))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$eq")
                {
                    if (!value.Equals(this.EvaluateMember(key)))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$in")
                {
                    if (!((IList)this.GetMember(key)).Contains(value))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$nin")
                {
                    if (((IList)this.GetMember(key)).Contains(value))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$not")
                {
                    if (((DynamicObject)this.GetMember(key)).Match(dynobj, name))
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (key == "$or")
                {
                    bool result = false;
                    foreach (var dobj in (IEnumerable <DynamicObject>) this.GetMember(key))
                    {
                        if (dobj.Match(dynobj, name))
                        {
                            result = true;
                            break;
                        }
                    }

                    if (!result)
                    {
                        return(false);
                    }

                    continue;
                }

                if (key == "$nor")
                {
                    foreach (var dobj in (IEnumerable <DynamicObject>) this.GetMember(key))
                    {
                        if (dobj.Match(dynobj, name))
                        {
                            return(false);
                        }
                    }
                    continue;
                }

                if (key == "$and")
                {
                    foreach (var dobj in (IEnumerable <DynamicObject>) this.GetMember(key))
                    {
                        if (!dobj.Match(dynobj, name))
                        {
                            return(false);
                        }
                    }
                    continue;
                }

                if (key == "$type")
                {
                    Type type = (Type)this.GetMember(key);

                    if (!type.IsInstanceOfType(value))
                    {
                        return(false);
                    }

                    continue;
                }

                throw new InvalidOperationException(string.Format("Invalid operator '{0}'", key));
            }

            return(true);
        }
コード例 #2
0
 public DynamicDocument FindOne(DynamicObject query = null, DynamicObject projection = null)
 {
     return(this.Find(query, projection).FirstOrDefault());
 }
コード例 #3
0
        public DynamicObject Project(DynamicObject projection)
        {
            IList <string> names = this.GetInitialNames();

            return(this.Project(projection, names));
        }
コード例 #4
0
 public IEnumerable <object> Distinct(string field, DynamicObject query = null)
 {
     return(this.Find(query).Select(d => d.GetMember(field)).Distinct());
 }