Permalink
Browse files

Merged from Steve Wagner

  • Loading branch information...
2 parents dd9bfaf + 34950dc commit e0b2cf0571dd104376aadb73727bd44021cdd717 @samus committed Mar 23, 2010
@@ -3,6 +3,7 @@
using NUnit.Framework;
using MongoDB.Driver.Bson;
+using System.Linq;
namespace MongoDB.Driver
{
@@ -292,6 +293,42 @@ private int CountDocs(ICursor cur)
}
[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"];
Document doc = new Document();
@@ -6,7 +6,7 @@
using NUnit.Framework;
using MongoDB.Driver.Bson;
-namespace MongoDB.Driver.Tests
+namespace MongoDB.Driver
{
[TestFixture]
public class TestOp
View
@@ -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)
{
@@ -4,7 +4,7 @@
namespace MongoDB.Driver
{
/// <summary>
- /// <see cref="IMongoCollection<T>"/>
+ ///
/// </summary>
public interface IMongoCollection{
string Name { get; }
@@ -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>
@@ -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>
@@ -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);
@@ -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>
@@ -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; }
@@ -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>
@@ -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);
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -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);

0 comments on commit e0b2cf0

Please sign in to comment.