Example #1
0
        // Inserts a new document created from a query and an update and returns it.
        BsonDocument InsertNewDocument(IMongoQuery query, IMongoUpdate update)
        {
            var document = new BsonDocument();

            UpdateCompiler.GetFunction((IConvertibleToBsonDocument)update, query.ToBsonDocument(), true)(document);
            InsertInternal(document);
            return(document);
        }
Example #2
0
        // public for tests
        public static Expression GetExpression(IConvertibleToBsonDocument update, IConvertibleToBsonDocument query, bool insert)
        {
            var        compiler   = new UpdateCompiler();
            Expression expression = Expression.Constant(compiler, typeof(UpdateCompiler));

            if (update == null)
            {
                return(expression);
            }

            if (insert)
            {
                expression = UpdateFromQueryExpression(expression, query);
            }

            var document = update.ToBsonDocument();

            bool isName = false;
            bool isOper = false;
            var  names  = new List <string>();

            foreach (var element in document)
            {
                var name = element.Name;
                if (name[0] == '$')
                {
                    if (isName)
                    {
                        ThrowMixedOperatorsAndNames();
                    }
                    isOper = true;

                    foreach (var fieldElement in element.Value.AsBsonDocument)
                    {
                        var fieldName = fieldElement.Name;
                        ValidateFieldName(fieldName, names);
                        expression = OperatorExpression(expression, name, Expression.Constant(fieldName, typeof(string)), fieldElement.Value, insert);
                    }
                }
                else                 //_131103_204607
                {
                    if (isOper)
                    {
                        ThrowMixedOperatorsAndNames();
                    }
                    isName = true;

                    ValidateFieldName(name, names);
                    expression = SetExpression(expression, Expression.Constant(name, typeof(string)), element.Value);
                }
            }

            return(expression);
        }
Example #3
0
        public object FindAndModifyAs(Type documentType, IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, IMongoFields fields, bool returnNew, bool upsert, out UpdateResult result)
        {
            foreach (var document in QueryCompiler.Query(Documents, Documents2, query, sortBy, 0, 0))
            {
                // if old is needed then deep(!) clone before update //_131103_185751
                BsonDocument output = null;
                if (!returnNew)
                {
                    output = document.DeepClone().AsBsonDocument;
                }

                UpdateDocument(document, UpdateCompiler.GetFunction((IConvertibleToBsonDocument)update, null, false));

                if (returnNew)
                {
                    output = document;
                }

                // project
                if (fields != null)
                {
                    var project = FieldCompiler.GetFunction(fields);
                    output = project(output);
                }

                // if old is needed then return it as already deep cloned
                result = new SimpleUpdateResult(1, true);
                if (!returnNew && documentType == typeof(Dictionary))
                {
                    return(new Dictionary(output));
                }
                else
                {
                    // deserialize to required type
                    return(BsonSerializer.Deserialize(output, documentType));
                }
            }

            // not found, insert
            if (upsert)
            {
                var document = InsertNewDocument(query, update);

                result = new SimpleUpdateResult(1, false);
                return(returnNew ? BsonSerializer.Deserialize(document, documentType) : null);
            }

            result = new SimpleUpdateResult(0, false);
            return(null);
        }
Example #4
0
        // public for tests
        public static Expression GetExpression(IConvertibleToBsonDocument update, IConvertibleToBsonDocument query, bool insert)
        {
            var compiler = new UpdateCompiler();
            Expression expression = Expression.Constant(compiler, typeof(UpdateCompiler));

            if (update == null)
                return expression;

            if (insert)
                expression = UpdateFromQueryExpression(expression, query);

            var document = update.ToBsonDocument();

            bool isName = false;
            bool isOper = false;
            var names = new List<string>();
            foreach (var element in document)
            {
                var name = element.Name;
                if (name[0] == '$')
                {
                    if (isName) ThrowMixedOperatorsAndNames();
                    isOper = true;

                    foreach (var fieldElement in element.Value.AsBsonDocument)
                    {
                        var fieldName = fieldElement.Name;
                        ValidateFieldName(fieldName, names);
                        expression = OperatorExpression(expression, name, Expression.Constant(fieldName, typeof(string)), fieldElement.Value, insert);
                    }
                }
                else //_131103_204607
                {
                    if (isOper) ThrowMixedOperatorsAndNames();
                    isName = true;

                    ValidateFieldName(name, names);
                    expression = SetExpression(expression, Expression.Constant(name, typeof(string)), element.Value);
                }
            }

            return expression;
        }
Example #5
0
        public WriteConcernResult Update(IMongoQuery query, IMongoUpdate update, UpdateFlags flags, WriteConcern writeConcern, bool needResult)
        {
            bool updatedExisting   = false;
            int  documentsAffected = 0;

            try
            {
                Func <BsonDocument, UpdateCompiler> function = null;
                foreach (var document in QueryDocuments(query))
                {
                    if (function == null)
                    {
                        function = UpdateCompiler.GetFunction((IConvertibleToBsonDocument)update, null, false);
                    }

                    UpdateDocument(document, function);

                    updatedExisting = true;
                    ++documentsAffected;

                    if ((flags & UpdateFlags.Multi) == 0)
                    {
                        break;
                    }
                }

                // not found and upsert
                if (function == null && (flags & UpdateFlags.Upsert) > 0)
                {
                    InsertNewDocument(query, update);
                    ++documentsAffected;
                }

                return(needResult ? new WriteConcernResult(NewResponse(documentsAffected, updatedExisting, null, null)) : null);
            }
            catch (Exception ex)
            {
                throw new FileWriteConcernException(ex.Message, new WriteConcernResult(NewResponse(documentsAffected, updatedExisting, ex.Message, null)));
            }
        }
Example #6
0
        static Expression UpdateFromQueryExpression(Expression that, IConvertibleToBsonDocument query)
        {
            if (query == null)
            {
                return(that);
            }

            foreach (var element in query.ToBsonDocument())
            {
                // v2.6 fails
                if (element.Name[0] == '$')
                {
                    throw new ArgumentException(string.Format(null, "Unknown top level query operator: ({0}).", element.Name));
                }

                var selector = element.Value;
                switch (selector.BsonType)
                {
                case BsonType.RegularExpression:
                    continue;

                case BsonType.Document:                         //TODO _140223_223449
                    var document = selector.AsBsonDocument;
                    if (document.ElementCount > 0)
                    {
                        var name = document.GetElement(0).Name;
                        if (name[0] == '$' && name != "$ref")
                        {
                            continue;
                        }
                    }
                    break;
                }

                that = UpdateCompiler.SetExpression(that, Expression.Constant(element.Name, typeof(string)), selector);
            }

            return(that);
        }