From 3efbbe24d3034236fe742ca8d64c4d42f015048c Mon Sep 17 00:00:00 2001 From: Steve Wagner Date: Thu, 10 Mar 2011 08:47:22 +0100 Subject: [PATCH] Add two test for FindAndModify and fix case where operates was wrapped in $set operator. --- .../IntegrationTests/TestCollection_1.cs | 39 +++++++++++++++++++ source/MongoDB/MongoCollection_1.cs | 11 ++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs b/source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs index 3df6b1d3..d6bea9dc 100644 --- a/source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs +++ b/source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs @@ -408,5 +408,44 @@ private class DeletesEntity Assert.IsNotNull(result); Assert.AreEqual(result["Existing"], 2.0); } + + public class Sequence + { + public string Name { get; set; } + public int Value { get; set; } + } + + [Test] + public void FindAndModifyWithoutOperatorWorks() + { + var collection = DB.GetCollection(); + + collection.Remove(new Document()); + collection.Insert(new Sequence { Name = "test", Value = 1 }); + + var spec = new Document().Add("Name", "test"); + var update = new Document("Value", 10); + + var document = collection.FindAndModify(update, spec, true); + + Assert.AreEqual("test", document.Name); + Assert.AreEqual(10, document.Value); + } + + [Test] + public void FindAndModifyIncOperatorWorks() + { + var collection = DB.GetCollection(); + + collection.Remove(new Document()); + collection.Insert(new Sequence { Name = "test", Value = 1 }); + + var spec = new Document().Add("Name", "test"); + var update = Mo.Inc("Value", 1); + + var document = collection.FindAndModify(update, spec, true); + + Assert.AreEqual(2, document.Value); + } } } \ No newline at end of file diff --git a/source/MongoDB/MongoCollection_1.cs b/source/MongoDB/MongoCollection_1.cs index a574c7aa..4c37f0bf 100644 --- a/source/MongoDB/MongoCollection_1.cs +++ b/source/MongoDB/MongoCollection_1.cs @@ -610,16 +610,13 @@ private object EnsureUpdateDocument(object document) { var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T)); - var foundOp = descriptor.GetMongoPropertyNames(document) - .Any(name => name.IndexOf('$') == 0); + var foundOp = document is Document && ( (Document)document ).Keys.Any(k => k.Contains("$")); if(foundOp == false) - { - //wrap document in a $set. - return new Document().Add("$set", document); - } + foundOp = descriptor.GetMongoPropertyNames(document) + .Any(name => name.Contains('$')); - return document; + return foundOp == false ? new Document().Add("$set", document) : document; } } }