Skip to content

Commit 3efbbe2

Browse files
committed
Add two test for FindAndModify and fix case where operates was wrapped in $set operator.
1 parent f6e51ad commit 3efbbe2

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

source/MongoDB.Tests/IntegrationTests/TestCollection_1.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,5 +408,44 @@ public void SaveUpdatesExistsingDocument(){
408408
Assert.IsNotNull(result);
409409
Assert.AreEqual(result["Existing"], 2.0);
410410
}
411+
412+
public class Sequence
413+
{
414+
public string Name { get; set; }
415+
public int Value { get; set; }
416+
}
417+
418+
[Test]
419+
public void FindAndModifyWithoutOperatorWorks()
420+
{
421+
var collection = DB.GetCollection<Sequence>();
422+
423+
collection.Remove(new Document());
424+
collection.Insert(new Sequence { Name = "test", Value = 1 });
425+
426+
var spec = new Document().Add("Name", "test");
427+
var update = new Document("Value", 10);
428+
429+
var document = collection.FindAndModify(update, spec, true);
430+
431+
Assert.AreEqual("test", document.Name);
432+
Assert.AreEqual(10, document.Value);
433+
}
434+
435+
[Test]
436+
public void FindAndModifyIncOperatorWorks()
437+
{
438+
var collection = DB.GetCollection<Sequence>();
439+
440+
collection.Remove(new Document());
441+
collection.Insert(new Sequence { Name = "test", Value = 1 });
442+
443+
var spec = new Document().Add("Name", "test");
444+
var update = Mo.Inc("Value", 1);
445+
446+
var document = collection.FindAndModify(update, spec, true);
447+
448+
Assert.AreEqual(2, document.Value);
449+
}
411450
}
412451
}

source/MongoDB/MongoCollection_1.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,16 +610,13 @@ private object EnsureUpdateDocument(object document)
610610
{
611611
var descriptor = _configuration.SerializationFactory.GetObjectDescriptor(typeof(T));
612612

613-
var foundOp = descriptor.GetMongoPropertyNames(document)
614-
.Any(name => name.IndexOf('$') == 0);
613+
var foundOp = document is Document && ( (Document)document ).Keys.Any(k => k.Contains("$"));
615614

616615
if(foundOp == false)
617-
{
618-
//wrap document in a $set.
619-
return new Document().Add("$set", document);
620-
}
616+
foundOp = descriptor.GetMongoPropertyNames(document)
617+
.Any(name => name.Contains('$'));
621618

622-
return document;
619+
return foundOp == false ? new Document().Add("$set", document) : document;
623620
}
624621
}
625622
}

0 commit comments

Comments
 (0)