コード例 #1
0
        public WriteConcernResult Update(T obj, WriteConcern concern = null)
        {
            ConcurrencyParameters <T> parameters = null;

            try
            {
                parameters = new ConcurrencyParameters <T>(obj, true);

                var update  = global::MongoDB.Driver.Builders.Update.Replace(parameters.Bson);
                var options = new MongoUpdateOptions {
                    WriteConcern = concern
                };

                var result = Collection.Update(parameters.QueryWithVersion, update, options);

                return(CheckConcurrencyResult(result, parameters));
            }
            catch (Exception)
            {
                if (parameters != null)
                {
                    obj.Version = parameters.RequestedVersion;
                }
                throw;
            }
        }
コード例 #2
0
        public WriteConcernResult Update(IMongoQuery query, UpdateBuilder update, MongoUpdateOptions updateOptions)
        {
            //TODO:  paul, deal with auditing
            IMongoQuery fixedQuery = MongoUtil.FormatIdElementForMongoQuery(query);

            return(Collection.Update(query, update, updateOptions));
        }
コード例 #3
0
        private void DeleteQueueIdFromPermission(string name)
        {
            var collection = _database.GetCollection <UserPermission>("UserPermission");

            UpdateBuilder ub = Update.Unset("Portal.DeliveryQueuePermissions." + name);

            MongoUpdateOptions options = new MongoUpdateOptions
            {
                Flags = UpdateFlags.Multi
            };

            collection.Update(Query <UserPermission> .Exists(e => e.Id), ub, options);
        }
コード例 #4
0
        public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
        {
            // starting with version 2.5.2 the server got stricter about dollars in element names
            // so this test should only be run when testing against older servers
            var server = Configuration.TestServer;

            if (server.BuildInfo.Version < new Version(2, 6, 0))
            {
                var database   = Configuration.TestDatabase;
                var collection = Configuration.TestCollection;
                collection.Drop();

                var document = new BsonDocument
                {
                    { "_id", 1 },
                    { "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
                };
                var insertOptions = new MongoInsertOptions {
                    CheckElementNames = false
                };
                collection.Insert(document, insertOptions);
                document = collection.FindOne();
                Assert.AreEqual(1, document["v"]["$x"].AsInt32);

                document["v"]["$x"] = 2;
                var query         = Query.EQ("_id", 1);
                var update        = Update.Replace(document);
                var updateOptions = new MongoUpdateOptions {
                    CheckElementNames = false
                };
                collection.Update(query, update, updateOptions);
                document = collection.FindOne();
                Assert.AreEqual(2, document["v"]["$x"].AsInt32);

                document["v"]["$x"] = 3;
                collection.Save(document, insertOptions);
                document = collection.FindOne();
                Assert.AreEqual(3, document["v"]["$x"].AsInt32);
            }
        }
コード例 #5
0
        public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
        {
            var server     = MongoServer.Create("mongodb://localhost/?safe=true;slaveOk=true");
            var database   = server["onlinetests"];
            var collection = database["test"];

            collection.Drop();

            var document = new BsonDocument {
                { "_id", 1 },
                { "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
            };
            var insertOptions = new MongoInsertOptions(collection)
            {
                CheckElementNames = false
            };

            collection.Insert(document, insertOptions);
            document = collection.FindOne();
            Assert.AreEqual(1, document["v"].AsBsonDocument["$x"].AsInt32);

            document["v"].AsBsonDocument["$x"] = 2;
            var query         = Query.EQ("_id", 1);
            var update        = Update.Replace(document);
            var updateOptions = new MongoUpdateOptions(collection)
            {
                CheckElementNames = false
            };

            collection.Update(query, update, updateOptions);
            document = collection.FindOne();
            Assert.AreEqual(2, document["v"].AsBsonDocument["$x"].AsInt32);

            document["v"].AsBsonDocument["$x"] = 3;
            collection.Save(document, insertOptions);
            document = collection.FindOne();
            Assert.AreEqual(3, document["v"].AsBsonDocument["$x"].AsInt32);
        }
コード例 #6
0
        public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
        {
            var server     = Configuration.TestServer;
            var database   = Configuration.TestDatabase;
            var collection = Configuration.TestCollection;

            collection.Drop();

            var document = new BsonDocument
            {
                { "_id", 1 },
                { "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
            };
            var insertOptions = new MongoInsertOptions {
                CheckElementNames = false
            };

            collection.Insert(document, insertOptions);
            document = collection.FindOne();
            Assert.AreEqual(1, document["v"].AsBsonDocument["$x"].AsInt32);

            document["v"].AsBsonDocument["$x"] = 2;
            var query         = Query.EQ("_id", 1);
            var update        = Update.Replace(document);
            var updateOptions = new MongoUpdateOptions {
                CheckElementNames = false
            };

            collection.Update(query, update, updateOptions);
            document = collection.FindOne();
            Assert.AreEqual(2, document["v"].AsBsonDocument["$x"].AsInt32);

            document["v"].AsBsonDocument["$x"] = 3;
            collection.Save(document, insertOptions);
            document = collection.FindOne();
            Assert.AreEqual(3, document["v"].AsBsonDocument["$x"].AsInt32);
        }
コード例 #7
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);
        }
コード例 #8
0
        public override WriteConcernResult Update(IMongoQuery query, IMongoUpdate update, MongoUpdateOptions options)
        {
            var sw = new Stopwatch();

            sw.Start();
            var result = base.Update(query, update, options);

            sw.Stop();

            var commandStringBuilder = new StringBuilder(1024);

            commandStringBuilder.AppendFormat("db.{0}.update(query, update", Name);

            var optionsList = new List <string>();

            if ((options.Flags & UpdateFlags.Upsert) == UpdateFlags.Upsert)
            {
                optionsList.Add("upsert: true");
            }

            if ((options.Flags & UpdateFlags.Multi) == UpdateFlags.Multi)
            {
                optionsList.Add("multi: true");
            }

            if (optionsList.Any())
            {
                commandStringBuilder.AppendFormat("{{ {0} }}", string.Join(", ", optionsList));
            }

            commandStringBuilder.Append(")");

            if (query != null)
            {
                commandStringBuilder.AppendFormat("\nquery = {0}", query.ToBsonDocument());
            }
            else
            {
                commandStringBuilder.Append("\nquery = {}");
            }

            if (update != null)
            {
                commandStringBuilder.AppendFormat("\nupdate = {0}", update.ToBsonDocument());
            }
            else
            {
                commandStringBuilder.Append("\nupdate = {}");
            }

            string commandString = commandStringBuilder.ToString();

            ProfilerUtils.AddMongoTiming(commandString, sw.ElapsedMilliseconds, ExecuteType.Update);

            return(result);
        }
コード例 #9
0
 public WriteConcernResult Update(IMongoQuery query, IMongoUpdate update, MongoUpdateOptions options, string dbName, string collectionName)
 {
     throw new NotImplementedException();
 }
コード例 #10
0
 public virtual WriteConcernResult Update(IMongoQuery query, IMongoUpdate update, MongoUpdateOptions options)
 {
     if (options != null)
     {
         return(this.context.GetCollection <TEntity>().Update(query, update, options));
     }
     return(this.context.GetCollection <TEntity>().Update(query, update));
 }
コード例 #11
0
 public SafeModeResult Update(IMongoQuery query, IMongoUpdate update, MongoUpdateOptions options)
 {
     return(_collection.Update(query, update, options));
 }