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

Commit

Permalink
Merged from Steve Wagner
Browse files Browse the repository at this point in the history
  • Loading branch information
samus committed Mar 23, 2010
2 parents dd9bfaf + 34950dc commit e0b2cf0
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 40 deletions.
37 changes: 37 additions & 0 deletions MongoDB.Net-Tests/TestCollection.cs
Expand Up @@ -3,6 +3,7 @@

using NUnit.Framework;
using MongoDB.Driver.Bson;
using System.Linq;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -291,6 +292,42 @@ private int CountDocs(ICursor cur)
Assert.AreEqual("Sam", result["First"]);
}

[Test]
public void TestSaveInsertDocumentIfNotExists(){
var saves = DB["updates"];
saves.Delete(new Document());

saves.Save(new Document("name", "Sam"));
saves.Save(new Document("name", "Steve"));

var array = saves.FindAll().Documents.ToArray();
Assert.AreEqual(2,array.Length);
Assert.AreEqual("Sam", array[0]["name"]);
Assert.AreEqual("Steve", array[1]["name"]);
}

[Test]
public void TestSaveInsertDocumentIfExists()
{
var saves = DB["updates"];
saves.Delete(new Document());

var document1 = new Document("name", "Alien1");
saves.Insert(document1);
var document2 = new Document("name", "Alien2");
saves.Insert(document2);

document1["name"] = "Sam";
saves.Save(document1);
document2["name"] = "Steve";
saves.Save(document2);

var array = saves.FindAll().Documents.ToArray();
Assert.AreEqual(2, array.Length);
Assert.AreEqual("Sam", array[0]["name"]);
Assert.AreEqual("Steve", array[1]["name"]);
}

[Test]
public void TestUpdateUpsertExisting(){
IMongoCollection updates = DB["updates"];
Expand Down
2 changes: 1 addition & 1 deletion MongoDB.Net-Tests/TestOp.cs
Expand Up @@ -6,7 +6,7 @@
using NUnit.Framework;
using MongoDB.Driver.Bson;

namespace MongoDB.Driver.Tests
namespace MongoDB.Driver
{
[TestFixture]
public class TestOp
Expand Down
23 changes: 12 additions & 11 deletions MongoDBDriver/Cursor`1.cs
Expand Up @@ -32,17 +32,18 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
Id = -1;
_connection = connection;
FullCollectionName = fullCollectionName;
}

/// <summary>
/// Initializes a new instance of the <see cref = "Cursor&lt;T&gt;" /> class.
/// </summary>
/// <param name = "connection">The conn.</param>
/// <param name = "fullCollectionName">Full name of the collection.</param>
/// <param name = "spec">The spec.</param>
/// <param name = "limit">The limit.</param>
/// <param name = "skip">The skip.</param>
/// <param name = "fields">The fields.</param>
}

/// <summary>
/// Initializes a new instance of the <see cref="Cursor&lt;T&gt;"/> class.
/// </summary>
/// <param name="serializationFactory">The serialization factory.</param>
/// <param name="connection">The conn.</param>
/// <param name="fullCollectionName">Full name of the collection.</param>
/// <param name="spec">The spec.</param>
/// <param name="limit">The limit.</param>
/// <param name="skip">The skip.</param>
/// <param name="fields">The fields.</param>
public Cursor(ISerializationFactory serializationFactory, Connection connection, string fullCollectionName, object spec, int limit, int skip, object fields)
: this(serializationFactory, connection, fullCollectionName)
{
Expand Down
2 changes: 1 addition & 1 deletion MongoDBDriver/IMongoCollection.cs
Expand Up @@ -4,7 +4,7 @@
namespace MongoDB.Driver
{
/// <summary>
/// <see cref="IMongoCollection<T>"/>
///
/// </summary>
public interface IMongoCollection{
string Name { get; }
Expand Down
20 changes: 11 additions & 9 deletions MongoDBDriver/IMongoCollection`1.cs
Expand Up @@ -256,6 +256,7 @@ public interface IMongoCollection<T> where T : class
/// </summary>
/// <param name = "document">The document.</param>
/// <param name = "safemode">if set to <c>true</c> [safemode].</param>
[Obsolete("Use Save instead")]
void Update(Document document, bool safemode);

/// <summary>
Expand All @@ -265,23 +266,23 @@ public interface IMongoCollection<T> where T : class
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
/// <remarks>See the safemode description in the class description</remarks>
[Obsolete("Use Save")]
[Obsolete("Use Save instead")]
void Update(object document, bool safemode);

/// <summary>
/// Inserts or updates a document in the database. If the document does not contain an _id one will be
/// generated and an upsert sent. Otherwise the document matching the _id of the document will be updated.
/// </summary>
/// <param name = "document">The document.</param>
[Obsolete("Use Save")]
[Obsolete("Use Save instead")]
void Update(Document document);

/// <summary>
/// Inserts or updates a document in the database. If the document does not contain an _id one will be
/// generated and an upsert sent. Otherwise the document matching the _id of the document will be updated.
/// </summary>
/// <param name = "document">The document.</param>
[Obsolete("Use Save")]
[Obsolete("Use Save instead")]
void Update(object document);

/// <summary>
Expand Down Expand Up @@ -400,21 +401,19 @@ public interface IMongoCollection<T> where T : class
/// Inserts or updates a document in the database. If the document does not contain an _id one will be
/// generated and an upsert sent. Otherwise the document matching the _id of the document will be updated.
/// </summary>
/// <param name = "document">The document.</param>
/// <param name="document">The document.</param>
/// <remarks>
/// The document will contain the _id that is saved to the database. This is really just an alias
/// to Update(Document) to maintain consistency between drivers.
/// The document will contain the _id that is saved to the database.
/// </remarks>
void Save(Document document);

/// <summary>
/// Inserts or updates a document in the database. If the document does not contain an _id one will be
/// generated and an upsert sent. Otherwise the document matching the _id of the document will be updated.
/// </summary>
/// <param name = "document">The document.</param>
/// <param name="document">The document.</param>
/// <remarks>
/// The document will contain the _id that is saved to the database. This is really just an alias
/// to Update(Document) to maintain consistency between drivers.
/// The document will contain the _id that is saved to the database.
/// </remarks>
void Save(object document);

Expand All @@ -423,6 +422,9 @@ public interface IMongoCollection<T> where T : class
/// </summary>
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
/// <remarks>
/// The document will contain the _id that is saved to the database.
/// </remarks>
void Save(Document document, bool safemode);

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MongoDBDriver/IMongoDatabase.cs
Expand Up @@ -24,7 +24,7 @@ public interface IMongoDatabase
DatabaseJavascript Javascript { get; }

/// <summary>
/// Gets the <see cref="MongoDB.Driver.IMongoCollection&lt;MongoDB.Driver.Document&gt;"/> with the specified name.
/// Gets the <see cref="MongoDB.Driver.IMongoCollection"/> with the specified name.
/// </summary>
/// <value></value>
IMongoCollection this[String name] { get; }
Expand Down
45 changes: 28 additions & 17 deletions MongoDBDriver/MongoCollection`1.cs
Expand Up @@ -412,18 +412,20 @@ public MongoCollection(ISerializationFactory serializationFactory, Connection co
/// </summary>
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
[Obsolete("Use Save instead")]
public void Update(Document document, bool safemode){
Update((object)document, safemode);
Save((object)document, safemode);
}

/// <summary>
/// Updates the specified document.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
public void Update(object document, bool safemode){
Update(document);
CheckError(safemode);
[Obsolete("Use Save instead")]
public void Update(object document, bool safemode)
{
Save(document, safemode);
}

/// <summary>
Expand Down Expand Up @@ -533,7 +535,12 @@ public MongoCollection(ISerializationFactory serializationFactory, Connection co
public void Update(object document, object selector, UpdateFlags flags){
var descriptor = _serializationFactory.GetBsonDescriptor(typeof(T), _connection);

var updateMessage = new UpdateMessage(descriptor) { FullCollectionName = FullName, Selector = selector, Document = document, Flags = (int)flags };
var updateMessage = new UpdateMessage(descriptor){
FullCollectionName = FullName,
Selector = selector,
Document = document,
Flags = (int)flags
};

try {
_connection.SendMessage(updateMessage);
Expand Down Expand Up @@ -616,7 +623,12 @@ public MongoCollection(ISerializationFactory serializationFactory, Connection co
/// </summary>
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
public void Save(Document document, bool safemode){
/// <remarks>
/// The document will contain the _id that is saved to the database. This is really just an alias
/// to Update(Document) to maintain consistency between drivers.
/// </remarks>
public void Save(Document document, bool safemode)
{
Save((object)document, safemode);
}

Expand All @@ -632,24 +644,18 @@ public MongoCollection(ISerializationFactory serializationFactory, Connection co
//Try to generate a selector using _id for an existing document.
//otherwise just set the upsert flag to 1 to insert and send onward.
var selector = new Document();
var upsert = UpdateFlags.Upsert;
var upsert = UpdateFlags.None;

var descriptor = _serializationFactory.GetObjectDescriptor(typeof(T));

var value = descriptor.GetPropertyValue(document, "_id");

if(value != null)
{
selector["_id"] = value;
}
else
{
if(value == null){
//Likely a new document
var id = descriptor.GenerateId(document);
descriptor.SetPropertyValue(document, "_id", id);
selector["_id"] = id;
descriptor.SetPropertyValue(document, "_id", value = Oid.NewOid());
upsert = UpdateFlags.Upsert;
}
selector["_id"] = value;

Update(document, selector, upsert);
}
Expand All @@ -659,7 +665,12 @@ public MongoCollection(ISerializationFactory serializationFactory, Connection co
/// </summary>
/// <param name="document">The document.</param>
/// <param name="safemode">if set to <c>true</c> [safemode].</param>
public void Save(object document,bool safemode){
/// <remarks>
/// The document will contain the _id that is saved to the database. This is really just an alias
/// to Update(Document) to maintain consistency between drivers.
/// </remarks>
public void Save(object document, bool safemode)
{
Save(document);
CheckError(safemode);
}
Expand Down
4 changes: 4 additions & 0 deletions MongoDBDriver/Protocol/UpdateMessage.cs
Expand Up @@ -17,6 +17,10 @@ namespace MongoDB.Driver.Protocol
/// </remarks>
public class UpdateMessage : RequestMessageBase
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateMessage"/> class.
/// </summary>
/// <param name="objectDescriptor">The object descriptor.</param>
public UpdateMessage(IBsonObjectDescriptor objectDescriptor)
: base(objectDescriptor){
Header = new MessageHeader(OpCode.Update);
Expand Down

0 comments on commit e0b2cf0

Please sign in to comment.