Exemplo n.º 1
0
        public WriteConcernResult Remove(IMongoQuery query, RemoveFlags flags, WriteConcern writeConcern, bool needResult)
        {
            var predicate = QueryCompiler.GetFunction(query.ToBsonDocument());

            int documentsAffected = 0;

            for (int i = 0; i < Documents.Count; ++i)
            {
                if (!predicate(Documents[i]))
                {
                    continue;
                }

                RemoveDocumentAt(i);

                ++documentsAffected;

                if ((flags & RemoveFlags.Single) > 0)
                {
                    break;
                }

                --i;
            }

            return(needResult ? new WriteConcernResult(NewResponse(documentsAffected, false, null, null)) : null);
        }
Exemplo n.º 2
0
        static void Pull(BsonDocument document, string name, BsonValue value, bool all)
        {
            var r = document.ResolvePath(name);

            if (r == null)
            {
                return;                 //_140322_065637
            }
            BsonValue vArray;

            if (r.Array != null)
            {
                vArray = r.Array[r.Index];
            }
            else if (!r.Document.TryGetValue(r.Key, out vArray))
            {
                return;
            }

            if (vArray.BsonType != BsonType.Array)
            {
                throw new InvalidOperationException(string.Format(null, @"Value ""{0}"" must be array.", name));
            }

            var array = vArray.AsBsonArray;

            if (!all && value.BsonType == BsonType.Document)
            {
                //_131130_103226 Created in Update.Pull(query)
                var wrapper = value as BsonDocumentWrapper;
                if (wrapper != null)
                {
                    value = (BsonValue)wrapper.WrappedObject;
                }

                var predicate = QueryCompiler.GetFunction(value.AsBsonDocument);
                for (int i = array.Count; --i >= 0;)
                {
                    var v = array[i];
                    if (v.BsonType == BsonType.Document && predicate(v.AsBsonDocument))
                    {
                        array.RemoveAt(i);
                    }
                }
            }
            else
            {
                BsonArray values = all ? value.AsBsonArray : null;
                for (int i = array.Count; --i >= 0;)
                {
                    if (values == null)
                    {
                        if (value.CompareTo(array[i]) == 0)
                        {
                            array.RemoveAt(i);
                        }
                    }
                    else
                    {
                        if (values.ContainsByCompareTo(array[i]))
                        {
                            array.RemoveAt(i);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 IEnumerable <BsonDocument> QueryDocuments(IMongoQuery query)
 {
     return(Documents.Where(QueryCompiler.GetFunction(query.ToBsonDocument())));
 }
Exemplo n.º 4
0
        }                                                                                                                                   //! DB text

        // Assume that keys are already unique, so we avoid many checks.
        FieldCompiler(IMongoFields fields)
        {
            var document = ((IConvertibleToBsonDocument)fields).ToBsonDocument();

            if (document.ElementCount == 0)
            {
                _All = true;
                return;
            }

            foreach (var e in document)
            {
                var name  = e.Name;
                var value = e.Value;
                if (value.IsNumeric)
                {
                    if (e.Value.ToInt32() != 0)
                    {
                        // include
                        if (_Exclude != null)
                        {
                            ThrowMix();
                        }

                        if (_Include == null)
                        {
                            _Include = new List <string>();
                        }

                        if (name != MyValue.Id)
                        {
                            _Include.Add(name);
                        }
                    }
                    else
                    {
                        // exclude
                        if (name == MyValue.Id)
                        {
                            _Id = false;
                            continue;
                        }

                        if (_Include != null)
                        {
                            ThrowMix();
                        }

                        if (_Exclude == null)
                        {
                            _Exclude = new List <string>();
                        }

                        _Exclude.Add(name);
                    }
                    continue;
                }

                BsonDocument selector;
                if (value.BsonType != BsonType.Document || (selector = value.AsBsonDocument).ElementCount != 1)
                {
                    throw new InvalidOperationException("Invalid type of fields.");
                }

                var element = selector.GetElement(0);
                var oper    = element.Name;
                var arg     = element.Value;

                // case slice
                if (oper == "$slice")
                {
                    if (arg.IsNumeric)
                    {
                        _Slice.Add(name, new int[] { 0, arg.ToInt32() });
                        continue;
                    }

                    BsonArray array;
                    if (arg.BsonType != BsonType.Array || (array = arg.AsBsonArray).Count != 2)
                    {
                        throw new InvalidOperationException("Invalid $slice argument.");
                    }

                    int n = array[1].ToInt32();
                    if (n <= 0)
                    {
                        throw new InvalidOperationException("$slice limit must be positive.");                         //! DB text
                    }
                    _Slice.Add(name, new int[] { array[0].ToInt32(), n });
                    continue;
                }

                // case match

                if (oper != "$elemMatch")
                {
                    throw new InvalidOperationException("Invalid field operator.");
                }

                if (arg.BsonType != BsonType.Document)
                {
                    throw new InvalidOperationException("Invalid field match argument.");
                }

                _ElemMatch.Add(name, QueryCompiler.GetFunction(arg.AsBsonDocument));
            }
        }
Exemplo n.º 5
0
 IEnumerable <BsonDocument> QueryDocuments(IMongoQuery query)
 {
     return(Documents.Where(QueryCompiler.GetFunction((IConvertibleToBsonDocument)query)));
 }