Skip to content

Commit 4645f3b

Browse files
author
rstam
committed
Added Replace method to Update<TDocument>.
1 parent a014fe5 commit 4645f3b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Driver/Builders/UpdateBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,17 @@ public static UpdateBuilder<TDocument> PushAll<TValue>(Expression<Func<TDocument
14571457
return new UpdateBuilder<TDocument>().PushAll(memberExpression, values);
14581458
}
14591459

1460+
/// <summary>
1461+
/// Replaces the entire document with a new document (the _id must remain the same).
1462+
/// </summary>
1463+
/// <param name="document">The replacement document.</param>
1464+
/// <returns>An UpdateWrapper.</returns>
1465+
public static IMongoUpdate Replace(TDocument document)
1466+
{
1467+
if (document == null) { throw new ArgumentNullException("document"); }
1468+
return UpdateWrapper.Create<TDocument>(document);
1469+
}
1470+
14601471
/// <summary>
14611472
/// Sets the value of the named element to a new value (see $set).
14621473
/// </summary>

DriverUnitTests/Builders/UpdateBuilderTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class UpdateBuilderTests
3131
{
3232
private class Test
3333
{
34+
public int Id = 0;
35+
3436
[BsonElement("x")]
3537
public int X = 0;
3638

@@ -416,6 +418,24 @@ public void TestPushAllWrapped()
416418
Assert.AreEqual(expected, update.ToJson());
417419
}
418420

421+
[Test]
422+
public void TestReplace()
423+
{
424+
var t = new Test { Id = 1, X = 2, Y = null, B = null };
425+
var update = Update.Replace(t);
426+
var expected = "{ \"_id\" : 1, \"x\" : 2, \"y\" : null, \"b\" : null }";
427+
Assert.AreEqual(expected, update.ToJson());
428+
}
429+
430+
[Test]
431+
public void TestReplace_Typed()
432+
{
433+
var t = new Test { Id = 1, X = 2, Y = null, B = null };
434+
var update = Update<Test>.Replace(t);
435+
var expected = "{ \"_id\" : 1, \"x\" : 2, \"y\" : null, \"b\" : null }";
436+
Assert.AreEqual(expected, update.ToJson());
437+
}
438+
419439
[Test]
420440
public void TestSet()
421441
{

0 commit comments

Comments
 (0)