Represents the options to use for an Update operation.
        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"]["$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);
        }
        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 { 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);
        }
        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);
            }
        }
예제 #4
0
        public void TestUpdate()
        {
            _collection.Drop();
            _collection.Insert(new BsonDocument("x", 1));
            var options = new MongoUpdateOptions { BypassDocumentValidation = true };

            var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2), options);

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 1,
                UpdatedExisting = true
            };
            CheckExpectedResult(expectedResult, result);

            var document = _collection.FindOne();
            Assert.Equal(2, document["x"].AsInt32);
            Assert.Equal(1, _collection.Count());
        }
예제 #5
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;
        }