Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Bug fix for save and update(doc)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Corder authored and Samuel Corder committed Apr 1, 2010
1 parent 2b8934f commit 5c79555
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
18 changes: 16 additions & 2 deletions MongoDB.Net-Tests/TestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TestCollection : MongoTestBase

public override string TestCollections {
get {
return "inserts,updates,counts,counts_spec,finds,charreads";
return "inserts,updates,counts,counts_spec,finds,charreads,saves";
}
}

Expand Down Expand Up @@ -313,7 +313,21 @@ public void TestUpdateUpsertExisting(){
Assert.AreEqual("Matt", result["First"]);

}

[Test]
public void TestSave(){
IMongoCollection saves = DB["saves"];
string[] vals = {"a","b","c","d"};
foreach(var v in vals){
saves.Save(new Document(){{"value", v}});
}
Assert.AreEqual(vals.Length, saves.Count());
Document d = saves.FindOne(new Document(){{"value", "b"}});
d["value"] = "b2";
saves.Save(d);

Assert.AreEqual(1, saves.Count(new Document(){{"value", "b2"}}));

}
[Test]
public void TestUpdateMany(){
IMongoCollection updates = DB["updates"];
Expand Down
29 changes: 16 additions & 13 deletions MongoDBDriver/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,6 @@ public void Update (Document doc, bool safemode){
/// to Update(Document) to maintain consistency between drivers.
/// </remarks>
public void Save(Document doc){
Update(doc);
}

/// <summary>
/// Updates a document with the data in doc as found by the selector.
/// </summary>
/// <remarks>
/// _id will be used in the document to create a selector. If it isn't in
/// the document then it is assumed that the document is new and an upsert is sent to the database
/// instead.
/// </remarks>
public void Update(Document doc){
//Try to generate a selector using _id for an existing document.
//otherwise just set the upsert flag to 1 to insert and send onward.
Document selector = new Document();
Expand All @@ -233,12 +221,27 @@ public void Update(Document doc){
selector["_id"] = doc["_id"];
}else{
//Likely a new document
doc.Prepend("_id",Oid.NewOid());
Oid id = Oid.NewOid();
selector["_id"] = id;
doc.Prepend("_id",id);
upsert = 1;
}
this.Update(doc, selector, upsert);
}

/// <summary>
/// Updates a document with the data in doc as found by the selector.
/// </summary>
/// <remarks>
/// _id will be used in the document to create a selector. If it isn't in
/// the document then it is assumed that the document is new and an upsert is sent to the database
/// instead.
/// </remarks>
[Obsolete("Switch to Save(Document)")]
public void Update(Document doc){
Save(doc);
}

public void Update (Document doc, Document selector, bool safemode){
Update(doc, selector,0,safemode);
}
Expand Down
3 changes: 2 additions & 1 deletion MongoDBDriver/IMongoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public interface IMongoCollection
void Insert (Document doc);
void Insert (Document doc, bool safemode);
void Insert (IEnumerable<Document> docs);
void Insert (IEnumerable<Document> docs, bool safemode);
void Insert (IEnumerable<Document> docs, bool safemode);
void Delete (Document selector);
void Delete (Document selector, bool safemode);
void Save(Document doc);
void Update (Document doc);
void Update (Document doc, Document selector);
void Update (Document doc, Document selector, int upsert);
Expand Down

0 comments on commit 5c79555

Please sign in to comment.