Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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)));
            }
        }