Skip to content

Commit

Permalink
Add two test for FindAndModify and fix case where operates was wrappe…
Browse files Browse the repository at this point in the history
…d in $set operator.
  • Loading branch information
lanwin committed Mar 10, 2011
1 parent f6e51ad commit 3efbbe2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
39 changes: 39 additions & 0 deletions source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs
Expand Up @@ -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<Sequence>();

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<Sequence>();

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);
}
}
}
11 changes: 4 additions & 7 deletions source/MongoDB/MongoCollection_1.cs
Expand Up @@ -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;
}
}
}

0 comments on commit 3efbbe2

Please sign in to comment.