Exemplo n.º 1
0
        public bool Replace(String key, object document)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            _metadata.SetDocumentId(document, key);
            var version = _metadata.GetDocumentVersion(document);

            var updateOptions = new MongoUpdateOptions
            {
                Flags = UpdateFlags.None, WriteConcern = WriteConcern.Acknowledged
            };

            IMongoQuery query = Query.EQ("_id", key);

            if (version != null)
            {
                // Increment version of document
                _metadata.SetDocumentVersion(document, version.Value + 1);

                var versionProperty = _metadata.GetDocumentVersionPropertyInfo(_documentType);
                query = Query.And(query, Query.EQ(versionProperty.Name, version));
            }

            var update = Update.Replace(_documentType, document);

            var result = _collection.Update(query, update, updateOptions);

            return(result.DocumentsAffected > 0);
        }