Browse files

Add some obsolete attribute.

Try fix collection.Save. Dose not inserted new documents. Should work new like shell save.
Add and fix a few docs.
  • Loading branch information...
1 parent 0699951 commit 34950dcff6fa24b6b428d200f2c4e06622c0d98c @lanwin lanwin committed Mar 22, 2010
View
37 MongoDB.Net-Tests/TestCollection.cs
@@ -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();
View
32 MongoDBDriver/Generic/Collection.cs
@@ -411,7 +411,8 @@ public Collection(ISerializationFactory serializationFactory, Connection connect
/// Updates the specified document.
/// </summary>
/// <param name="document">The document.</param>
- /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ [Obsolete("Use Save instead")]
public void Update(Document document, bool safemode){
Save((object)document, safemode);
}
@@ -420,8 +421,10 @@ public Collection(ISerializationFactory serializationFactory, Connection connect
/// 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){
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ [Obsolete("Use Save instead")]
+ public void Update(object document, bool safemode)
+ {
Save(document, safemode);
}
@@ -433,8 +436,10 @@ public Collection(ISerializationFactory serializationFactory, Connection connect
/// _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 document){
+ /// </remarks>
+ [Obsolete("Use Save instead")]
+ public void Update(Document document)
+ {
Save((object)document);
}
@@ -446,8 +451,10 @@ public Collection(ISerializationFactory serializationFactory, Connection connect
/// _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(object document){
+ /// </remarks>
+ [Obsolete("Use Save instead")]
+ public void Update(object document)
+ {
Save(document);
}
@@ -530,7 +537,12 @@ public Collection(ISerializationFactory serializationFactory, Connection connect
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);
@@ -642,14 +654,14 @@ public void Save(Document document, bool safemode)
if(value != null)
{
- selector["_id"] = value;
}
else
{
//Likely a new document
- descriptor.SetPropertyValue(document, "_id", Oid.NewOid());
+ descriptor.SetPropertyValue(document, "_id", value = Oid.NewOid());
upsert = UpdateFlags.Upsert;
}
+ selector["_id"] = value;
Update(document, selector, upsert);
}
View
23 MongoDBDriver/Generic/Cursor.cs
@@ -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)
{
View
13 MongoDBDriver/Generic/IMongoCollection.cs
@@ -401,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);
@@ -424,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>
View
4 MongoDBDriver/IMongoCollection.cs
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
-using MongoDB.Driver.Generic;
-
namespace MongoDB.Driver
{
/// <summary>
- /// <see cref="IMongoCollection<T>"/>
+ ///
/// </summary>
public interface IMongoCollection{
string Name { get; }
View
2 MongoDBDriver/IMongoDatabase.cs
@@ -26,7 +26,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; }
View
4 MongoDBDriver/Protocol/UpdateMessage.cs
@@ -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 34950dc

Please sign in to comment.