Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mongo name put back in front of Collection and Database classes to av…

…oid clashing with Microsoft.VisualBasic.Collection.

Bug fixed in Collection.Save where documents were not always being upserted properly.
  • Loading branch information...
commit dd9bfaf175e154c2e5e85b32b356c862b360d814 1 parent 24fb5dc
@samus authored
Showing with 1,403 additions and 1,391 deletions.
  1. +10 −10 MongoDB.Driver.Benchmark/Main.cs
  2. +1 −1  MongoDB.GridFS/GridFileInfo.cs
  3. +1 −1  MongoDB.Net-Tests/Serialization/ObjectArrayBuilderTests.cs
  4. +1 −1  MongoDB.Net-Tests/Serialization/ReflectionBuilderTests.cs
  5. +1 −1  MongoDB.Net-Tests/Serialization/ReflectionDescriptorTests.cs
  6. +1 −1  MongoDB.Net-Tests/Serialization/SerializationTestBase.cs
  7. +20 −2 MongoDB.Net-Tests/TestCollection.cs
  8. +93 −93 MongoDB.Net-Tests/TestCollectionMetaData.cs
  9. +40 −40 MongoDB.Net-Tests/TestMongo.cs
  10. +7 −4 MongoDBDriver.sln
  11. +102 −102 MongoDBDriver/CollectionMetaData.cs
  12. +150 −150 MongoDBDriver/DatabaseJavascript.cs
  13. +60 −60 MongoDBDriver/DatabaseMetaData.cs
  14. +1 −1  MongoDBDriver/IMongoDatabase.cs
  15. +103 −103 MongoDBDriver/Mongo.cs
  16. +4 −4 MongoDBDriver/{Collection.cs → MongoCollection.cs}
  17. +694 −707 MongoDBDriver/{Collection`1.cs → MongoCollection`1.cs}
  18. +3 −3 MongoDBDriver/MongoDB.Driver.csproj
  19. +8 −8 MongoDBDriver/{Database.cs → MongoDatabase.cs}
  20. +98 −98 examples/Simple/Main.cs
  21. +3 −0  examples/SimpleVB/Application.vb
  22. +2 −1  examples/SimpleVB/SimpleVB.vbproj
View
20 MongoDB.Driver.Benchmark/Main.cs
@@ -26,7 +26,7 @@ public static void Main (string[] args)
Mongo m = new Mongo();
m.Connect();
- Database db = m["benchmark"];
+ MongoDatabase db = m["benchmark"];
db.MetaData.DropDatabase();
Console.WriteLine("Starting Tests");
@@ -104,7 +104,7 @@ public static void Main (string[] args)
large.Add("harvested_words", harvestedWords);
}
#region Insert Tests
- static void RunInsertTest(string name, Database db, string col, Document doc, bool index, bool bulk){
+ static void RunInsertTest(string name, MongoDatabase db, string col, Document doc, bool index, bool bulk){
TimeSpan lowest = TimeSpan.MaxValue;
for(int i = 0; i < trials; i++){
SetupInsert(db,"col",index);
@@ -115,7 +115,7 @@ public static void Main (string[] args)
Console.Out.WriteLine(String.Format("{0}{1} {2}", name + new string('.', 55 - name.Length), opsSec, lowest));
}
- static void SetupInsert(Database db, string col, bool index){
+ static void SetupInsert(MongoDatabase db, string col, bool index){
try{
db.MetaData.DropCollection(col);
if(index){
@@ -127,7 +127,7 @@ public static void Main (string[] args)
}
}
- static TimeSpan TimeInsert(Database db, string col, Document doc, bool bulk){
+ static TimeSpan TimeInsert(MongoDatabase db, string col, Document doc, bool bulk){
DateTime start = DateTime.Now;
if(bulk){
DoBulkInsert(db,col,doc, batchSize);
@@ -139,7 +139,7 @@ public static void Main (string[] args)
return t;
}
- static void DoInsert(Database db, string col, Document doc){
+ static void DoInsert(MongoDatabase db, string col, Document doc){
for(int i = 0; i < perTrial; i++){
Document ins = new Document();
doc.CopyTo(ins);
@@ -148,7 +148,7 @@ public static void Main (string[] args)
}
}
- static void DoBulkInsert(Database db, string col, Document doc, int size){
+ static void DoBulkInsert(MongoDatabase db, string col, Document doc, int size){
for(int i = 0; i < perTrial / size; i++){
Document[] docs = new Document[size];
for(int f = 0; f < docs.Length; f++){
@@ -224,7 +224,7 @@ public static void Main (string[] args)
}
#region Find Tests
- static void RunFindTest(string name, Database db, string col, Document spec, bool range){
+ static void RunFindTest(string name, MongoDatabase db, string col, Document spec, bool range){
TimeSpan lowest = TimeSpan.MaxValue;
for(int i = 0; i < trials; i++){
TimeSpan ret = TimeFind(db, col, spec, range);
@@ -234,7 +234,7 @@ public static void Main (string[] args)
Console.Out.WriteLine(String.Format("{0}{1} {2}", name + new string('.', 55 - name.Length), opsSec, lowest));
}
- static TimeSpan TimeFind(Database db, string col,Document psec, bool range){
+ static TimeSpan TimeFind(MongoDatabase db, string col,Document psec, bool range){
DateTime start = DateTime.Now;
if(range){
DoFindOne(db,col,psec);
@@ -246,13 +246,13 @@ public static void Main (string[] args)
return t;
}
- static void DoFindOne(Database db, string col, Document spec){
+ static void DoFindOne(MongoDatabase db, string col, Document spec){
for(int i = 0; i < perTrial; i++){
db[col].FindOne(spec);
}
}
- static void DoFind(Database db, string col, Document spec){
+ static void DoFind(MongoDatabase db, string col, Document spec){
for(int i = 0; i < perTrial; i++){
ICursor cur = db[col].Find(spec);
foreach(Document d in cur.Documents){
View
2  MongoDB.GridFS/GridFileInfo.cs
@@ -92,7 +92,7 @@ public string FileName
if(gridFile.Exists(filename)) this.LoadFileData();
}
- public GridFileInfo(Database db, string filename){
+ public GridFileInfo(MongoDatabase db, string filename){
this.db = db;
this.bucket = "fs";
this.gridFile = new GridFile(db);
View
2  MongoDB.Net-Tests/Serialization/ObjectArrayBuilderTests.cs
@@ -4,7 +4,7 @@
using MongoDB.Driver.Serialization.Builders;
using NUnit.Framework;
-namespace MongoDB.Driver.Tests.Serialization
+namespace MongoDB.Driver.Serialization
{
[TestFixture]
public class ObjectArrayBuilderTests
View
2  MongoDB.Net-Tests/Serialization/ReflectionBuilderTests.cs
@@ -4,7 +4,7 @@
using System.Globalization;
using NUnit.Framework;
-namespace MongoDB.Driver.Tests.Serialization
+namespace MongoDB.Driver.Serialization
{
[TestFixture]
public class ReflectionBuilderTests : SerializationTestBase
View
2  MongoDB.Net-Tests/Serialization/ReflectionDescriptorTests.cs
@@ -2,7 +2,7 @@
using MongoDB.Driver.Serialization.Attributes;
using NUnit.Framework;
-namespace MongoDB.Driver.Tests.Serialization
+namespace MongoDB.Driver.Serialization
{
[TestFixture]
public class ReflectionDescriptorTests : SerializationTestBase
View
2  MongoDB.Net-Tests/Serialization/SerializationTestBase.cs
@@ -4,7 +4,7 @@
using MongoDB.Driver.Serialization;
using MongoDB.Driver.Tests.Bson;
-namespace MongoDB.Driver.Tests.Serialization
+namespace MongoDB.Driver.Serialization
{
public abstract class SerializationTestBase : BsonTestBase
{
View
22 MongoDB.Net-Tests/TestCollection.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using NUnit.Framework;
@@ -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";
}
}
@@ -395,5 +395,23 @@ private int CountDocs(ICursor cur)
IMongoCollection counts = DB["counts_wtf"];
Assert.AreEqual(0, counts.Count());
}
+
+ [Test]
+ public void TestSave(){
+ IMongoCollection saves = DB["saves"];
+ int count = 100;
+ for(int i = 0; i < count; i++){
+ saves.Save(new Document(){{"x", i},{"desc", "This document is number: " + i},{"y", 1}});
+ }
+ Assert.AreEqual(count, saves.Count(new Document(){{"y", 1}}));
+
+ using(var cur = saves.FindAll()){
+ foreach(var d in cur.Documents){
+ d["y"] = Convert.ToInt32(d["y"]) + 1;
+ saves.Save(d);
+ }
+ }
+ Assert.AreEqual(count, saves.Count(new Document(){{"y", 2}}));
+ }
}
}
View
186 MongoDB.Net-Tests/TestCollectionMetaData.cs
@@ -1,102 +1,102 @@
-using System;
-using System.Collections.Generic;
-
-using NUnit.Framework;
-using MongoDB.Driver.Bson;
-
-namespace MongoDB.Driver
-{
- [TestFixture]
- public class TestCollectionMetaData : MongoTestBase
- {
- Database adminDb;
-
- public override string TestCollections {
- get {
- return "indextests,rename,renamed";
- }
- }
-
+using System;
+using System.Collections.Generic;
+
+using NUnit.Framework;
+using MongoDB.Driver.Bson;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestCollectionMetaData : MongoTestBase
+ {
+ MongoDatabase adminDb;
+
+ public override string TestCollections {
+ get {
+ return "indextests,rename,renamed";
+ }
+ }
+
public override void OnInit (){
- IMongoCollection its = DB["indextests"];
- its.Insert(createDoc("S","A","Anderson","OH"));
- its.Insert(createDoc("T","B","Delhi","OH"));
- its.Insert(createDoc("F","B","Cincinnati","OH"));
- its.Insert(createDoc("U","D","Newtown","OH"));
- its.Insert(createDoc("J","E","Newport","KY"));
-
- adminDb = DB.GetSisterDatabase("admin");
- //adminDb.MetaData.AddUser(adminuser, adminpass);
- }
-
- public override void OnDispose (){
- //adminDb.MetaData.RemoveUser(adminuser);
+ IMongoCollection its = DB["indextests"];
+ its.Insert(createDoc("S","A","Anderson","OH"));
+ its.Insert(createDoc("T","B","Delhi","OH"));
+ its.Insert(createDoc("F","B","Cincinnati","OH"));
+ its.Insert(createDoc("U","D","Newtown","OH"));
+ its.Insert(createDoc("J","E","Newport","KY"));
+
+ adminDb = DB.GetSisterDatabase("admin");
+ //adminDb.MetaData.AddUser(adminuser, adminpass);
+ }
+
+ public override void OnDispose (){
+ //adminDb.MetaData.RemoveUser(adminuser);
+ }
+
+ [Test]
+ public void TestGetOptions(){
+ CollectionMetaData cmd = DB["reads"].MetaData;
+ Document options = cmd.Options;
+ Assert.IsNotNull(options);
+ }
+
+ [Test]
+ public void TestGetIndexes(){
+ CollectionMetaData cmd = DB["indextests"].MetaData;
+ Dictionary<string, Document> indexes = cmd.Indexes;
+
+ Assert.IsNotNull(indexes);
+ Assert.IsTrue(indexes.Count > 0, "Should have been at least one index found.");
+ foreach(string key in indexes.Keys){
+ System.Console.WriteLine(String.Format("Key: {0} Value: {1}", key, indexes[key]));
+ }
}
-
- [Test]
- public void TestGetOptions(){
- CollectionMetaData cmd = DB["reads"].MetaData;
- Document options = cmd.Options;
- Assert.IsNotNull(options);
- }
-
- [Test]
- public void TestGetIndexes(){
- CollectionMetaData cmd = DB["indextests"].MetaData;
- Dictionary<string, Document> indexes = cmd.Indexes;
-
- Assert.IsNotNull(indexes);
- Assert.IsTrue(indexes.Count > 0, "Should have been at least one index found.");
- foreach(string key in indexes.Keys){
- System.Console.WriteLine(String.Format("Key: {0} Value: {1}", key, indexes[key]));
- }
- }
-
- [Test]
- public void TestCreateIndex(){
+
+ [Test]
+ public void TestCreateIndex(){
CollectionMetaData cmd = DB["indextests"].MetaData;
- cmd.CreateIndex("lastnames", new Document().Add("lname", IndexOrder.Ascending), false);
- Dictionary<string, Document> indexes = cmd.Indexes;
- Assert.IsNotNull(indexes["lastnames"]);
- }
-
- [Test]
- public void TestCreateIndexNoNames(){
+ cmd.CreateIndex("lastnames", new Document().Add("lname", IndexOrder.Ascending), false);
+ Dictionary<string, Document> indexes = cmd.Indexes;
+ Assert.IsNotNull(indexes["lastnames"]);
+ }
+
+ [Test]
+ public void TestCreateIndexNoNames(){
CollectionMetaData cmd = DB["indextests"].MetaData;
- cmd.CreateIndex(new Document().Add("lname", IndexOrder.Ascending).Add("fname", IndexOrder.Ascending), true);
- Dictionary<string, Document> indexes = cmd.Indexes;
- Assert.IsNotNull(indexes["_lname_fname_unique_"]);
- }
-
- [Test]
- public void TestDropIndex(){
+ cmd.CreateIndex(new Document().Add("lname", IndexOrder.Ascending).Add("fname", IndexOrder.Ascending), true);
+ Dictionary<string, Document> indexes = cmd.Indexes;
+ Assert.IsNotNull(indexes["_lname_fname_unique_"]);
+ }
+
+ [Test]
+ public void TestDropIndex(){
CollectionMetaData cmd = DB["indextests"].MetaData;
- cmd.CreateIndex("firstnames", new Document().Add("fname", IndexOrder.Ascending), false);
- Dictionary<string, Document> indexes = cmd.Indexes;
- Assert.IsNotNull(indexes["firstnames"]);
- cmd.DropIndex("firstnames");
- Assert.IsFalse(cmd.Indexes.ContainsKey("firstnames"));
- }
-
- [Test]
+ cmd.CreateIndex("firstnames", new Document().Add("fname", IndexOrder.Ascending), false);
+ Dictionary<string, Document> indexes = cmd.Indexes;
+ Assert.IsNotNull(indexes["firstnames"]);
+ cmd.DropIndex("firstnames");
+ Assert.IsFalse(cmd.Indexes.ContainsKey("firstnames"));
+ }
+
+ [Test]
public void TestRename(){
- DB["rename"].Insert(new Document(){{"test", "rename"}});
+ DB["rename"].Insert(new Document(){{"test", "rename"}});
Assert.AreEqual(1, DB["rename"].Count());
CollectionMetaData cmd = DB["rename"].MetaData;
- cmd.Rename("renamed");
- Assert.IsFalse(DB.GetCollectionNames().Contains(DB.Name + ".rename"), "Shouldn't have found collection");
- Assert.IsTrue(DB.GetCollectionNames().Contains(DB.Name + ".renamed"),"Should have found collection");
+ cmd.Rename("renamed");
+ Assert.IsFalse(DB.GetCollectionNames().Contains(DB.Name + ".rename"), "Shouldn't have found collection");
+ Assert.IsTrue(DB.GetCollectionNames().Contains(DB.Name + ".renamed"),"Should have found collection");
Assert.AreEqual(1, DB["renamed"].Count());
- }
-
- protected Document createDoc(string fname, string lname, string city, string state){
- Document doc = new Document();
- doc["fname"] = fname;
- doc["lname"] = lname;
- doc["city"] = city;
- doc["state"] = state;
- return doc;
- }
-
- }
-}
+ }
+
+ protected Document createDoc(string fname, string lname, string city, string state){
+ Document doc = new Document();
+ doc["fname"] = fname;
+ doc["lname"] = lname;
+ doc["city"] = city;
+ doc["state"] = state;
+ return doc;
+ }
+
+ }
+}
View
80 MongoDB.Net-Tests/TestMongo.cs
@@ -1,40 +1,40 @@
-using System;
-using System.Configuration;
-
-using NUnit.Framework;
-
-using MongoDB.Driver;
-
-namespace MongoDB.Driver
-{
- [TestFixture()]
- public class TestMongo
- {
- string connectionString = ConfigurationManager.AppSettings["tests"];
- [Test()]
- public void TestDefaults()
- {
- Mongo m = new Mongo(); //Connection string not needed since connect not called and it would screw up the test.
- Assert.AreEqual(string.Empty, m.ConnectionString);
- }
-
- [Test()]
- public void TestExplicitConnection(){
- Mongo m = new Mongo(connectionString);
- Assert.IsTrue(m.Connect());
- }
-
- [Test()]
- public void TestThatConnectMustBeCalled(){
- Mongo m = new Mongo(connectionString);
- bool thrown = false;
- try{
- Database db = m["admin"];
- db["$cmd"].FindOne(new Document().Add("listDatabases", 1.0));
- }catch(MongoCommException){
- thrown = true;
- }
- Assert.IsTrue(thrown, "MongoComException not thrown");
- }
- }
-}
+using System;
+using System.Configuration;
+
+using NUnit.Framework;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver
+{
+ [TestFixture()]
+ public class TestMongo
+ {
+ string connectionString = ConfigurationManager.AppSettings["tests"];
+ [Test()]
+ public void TestDefaults()
+ {
+ Mongo m = new Mongo(); //Connection string not needed since connect not called and it would screw up the test.
+ Assert.AreEqual(string.Empty, m.ConnectionString);
+ }
+
+ [Test()]
+ public void TestExplicitConnection(){
+ Mongo m = new Mongo(connectionString);
+ Assert.IsTrue(m.Connect());
+ }
+
+ [Test()]
+ public void TestThatConnectMustBeCalled(){
+ Mongo m = new Mongo(connectionString);
+ bool thrown = false;
+ try{
+ MongoDatabase db = m["admin"];
+ db["$cmd"].FindOne(new Document().Add("listDatabases", 1.0));
+ }catch(MongoCommException){
+ thrown = true;
+ }
+ Assert.IsTrue(thrown, "MongoComException not thrown");
+ }
+ }
+}
View
11 MongoDBDriver.sln
@@ -1,6 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
+# SharpDevelop 3.1.1.5327
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Driver", "MongoDBDriver\MongoDB.Driver.csproj", "{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}"
EndProject
Project("{9344bdbb-3e7f-41fc-a0dd-8665d75ee146}") = "Packages", "Packages.mdproj", "{502F3381-58AA-461B-B9D8-12578A588C61}"
@@ -69,10 +70,8 @@ Global
{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.Build.0 = Release|Any CPU
- EndGlobalSection
- GlobalSection(NestedProjects) = preSolution
- {1BAAE3D8-7720-4AA5-9335-E59824E7B667} = {4345382B-FAA2-46E2-99CF-C90ACA2DD574}
- {131BDB5F-5C6F-4AC7-B03E-394B1B75E120} = {4345382B-FAA2-46E2-99CF-C90ACA2DD574}
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = MongoDBDriver\MongoDB.Driver.csproj
@@ -113,4 +112,8 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
+ GlobalSection(NestedProjects) = preSolution
+ {131BDB5F-5C6F-4AC7-B03E-394B1B75E120} = {4345382B-FAA2-46E2-99CF-C90ACA2DD574}
+ {1BAAE3D8-7720-4AA5-9335-E59824E7B667} = {4345382B-FAA2-46E2-99CF-C90ACA2DD574}
+ EndGlobalSection
EndGlobal
View
204 MongoDBDriver/CollectionMetaData.cs
@@ -1,108 +1,108 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
+using System;
+using System.Collections.Generic;
+using System.Text;
using MongoDB.Driver.Connections;
-using MongoDB.Driver.Serialization;
-
-namespace MongoDB.Driver
-{
- /// <summary>
- /// Lazily loaded meta data on the collection.
- /// </summary>
- public class CollectionMetaData
- {
- private string fullName;
- private string name;
- private Database db;
-
- public CollectionMetaData (ISerializationFactory serializationFactory, string dbName, string name, Connection conn){
- this.fullName = dbName + "." + name;
- this.name = name;
- this.db = new Database (serializationFactory, conn, dbName);
- }
-
- private Document options = null;
- public Document Options {
- get {
- if (options != null)
+using MongoDB.Driver.Serialization;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Lazily loaded meta data on the collection.
+ /// </summary>
+ public class CollectionMetaData
+ {
+ private string fullName;
+ private string name;
+ private MongoDatabase db;
+
+ public CollectionMetaData (ISerializationFactory serializationFactory, string dbName, string name, Connection conn){
+ this.fullName = dbName + "." + name;
+ this.name = name;
+ this.db = new MongoDatabase (serializationFactory, conn, dbName);
+ }
+
+ private Document options = null;
+ public Document Options {
+ get {
+ if (options != null)
return options;
- Document doc = db["system.namespaces"].FindOne(new Document().Add("name", this.fullName));
- if (doc == null)
- doc = new Document ();
- if (doc.Contains ("create"))
- doc.Remove ("create");
- //Not sure why this is here. The python driver has it.
- this.options = doc;
- return this.options;
- }
- }
-
- private bool gotIndexes = false;
- private Dictionary<string, Document> indexes = new Dictionary<string, Document> ();
- public Dictionary<string, Document> Indexes {
- get {
- if (gotIndexes)
- return indexes;
-
+ Document doc = db["system.namespaces"].FindOne(new Document().Add("name", this.fullName));
+ if (doc == null)
+ doc = new Document ();
+ if (doc.Contains ("create"))
+ doc.Remove ("create");
+ //Not sure why this is here. The python driver has it.
+ this.options = doc;
+ return this.options;
+ }
+ }
+
+ private bool gotIndexes = false;
+ private Dictionary<string, Document> indexes = new Dictionary<string, Document> ();
+ public Dictionary<string, Document> Indexes {
+ get {
+ if (gotIndexes)
+ return indexes;
+
indexes.Clear ();
- var docs = db["system.indexes"].Find(new Document().Add("ns", this.fullName));
- foreach (Document doc in docs.Documents) {
- indexes.Add ((string)doc["name"], doc);
- }
-
- return indexes;
- }
- }
-
- public void CreateIndex (string name, Document fieldsAndDirections, bool unique){
- Document index = new Document ();
- index["name"] = name;
- index["ns"] = this.fullName;
- index["key"] = fieldsAndDirections;
- index["unique"] = unique;
- db["system.indexes"].Insert (index);
- this.refresh ();
- }
-
- public void CreateIndex (Document fieldsAndDirections, bool unique){
- string name = this.generateIndexName (fieldsAndDirections, unique);
- this.CreateIndex (name, fieldsAndDirections, unique);
- }
-
- public void DropIndex (string name){
+ var docs = db["system.indexes"].Find(new Document().Add("ns", this.fullName));
+ foreach (Document doc in docs.Documents) {
+ indexes.Add ((string)doc["name"], doc);
+ }
+
+ return indexes;
+ }
+ }
+
+ public void CreateIndex (string name, Document fieldsAndDirections, bool unique){
+ Document index = new Document ();
+ index["name"] = name;
+ index["ns"] = this.fullName;
+ index["key"] = fieldsAndDirections;
+ index["unique"] = unique;
+ db["system.indexes"].Insert (index);
+ this.refresh ();
+ }
+
+ public void CreateIndex (Document fieldsAndDirections, bool unique){
+ string name = this.generateIndexName (fieldsAndDirections, unique);
+ this.CreateIndex (name, fieldsAndDirections, unique);
+ }
+
+ public void DropIndex (string name){
Document cmd = new Document ();
- cmd.Add("deleteIndexes", this.name).Add("index", name);
- db.SendCommand (cmd);
- this.refresh ();
- }
-
- public void Rename (string newName){
- if (string.IsNullOrEmpty (newName))
- throw new ArgumentException ("Name must not be null or empty", "newName");
-
+ cmd.Add("deleteIndexes", this.name).Add("index", name);
+ db.SendCommand (cmd);
+ this.refresh ();
+ }
+
+ public void Rename (string newName){
+ if (string.IsNullOrEmpty (newName))
+ throw new ArgumentException ("Name must not be null or empty", "newName");
+
Document cmd = new Document ();
- cmd.Add("renameCollection", fullName).Add("to", db.Name + "." + newName);
- db.GetSisterDatabase ("admin").SendCommand (cmd);
- this.refresh ();
- }
-
- public void refresh (){
- indexes.Clear ();
- gotIndexes = false;
- options = null;
- }
-
- protected string generateIndexName (Document fieldsAndDirections, bool unique){
- StringBuilder sb = new StringBuilder ("_");
- foreach (string key in fieldsAndDirections.Keys) {
- sb.Append (key).Append ("_");
- }
- if (unique)
- sb.Append ("unique_");
-
- return sb.ToString ();
- }
-
- }
-}
+ cmd.Add("renameCollection", fullName).Add("to", db.Name + "." + newName);
+ db.GetSisterDatabase ("admin").SendCommand (cmd);
+ this.refresh ();
+ }
+
+ public void refresh (){
+ indexes.Clear ();
+ gotIndexes = false;
+ options = null;
+ }
+
+ protected string generateIndexName (Document fieldsAndDirections, bool unique){
+ StringBuilder sb = new StringBuilder ("_");
+ foreach (string key in fieldsAndDirections.Keys) {
+ sb.Append (key).Append ("_");
+ }
+ if (unique)
+ sb.Append ("unique_");
+
+ return sb.ToString ();
+ }
+
+ }
+}
View
300 MongoDBDriver/DatabaseJavascript.cs
@@ -1,162 +1,162 @@
-using System;
-using System.Collections.Generic;
-
-namespace MongoDB.Driver{
-
- /// <summary>
- /// Encapsulates and provides access to the serverside javascript stored in db.system.js.
- /// </summary>
- public class DatabaseJavascript : ICollection<Document>
- {
- private IMongoCollection js;
-
- internal DatabaseJavascript (Database db){
- this.js = db["system.js"];
+using System;
+using System.Collections.Generic;
+
+namespace MongoDB.Driver{
+
+ /// <summary>
+ /// Encapsulates and provides access to the serverside javascript stored in db.system.js.
+ /// </summary>
+ public class DatabaseJavascript : ICollection<Document>
+ {
+ private IMongoCollection js;
+
+ internal DatabaseJavascript (MongoDatabase db){
+ this.js = db["system.js"];
//Needed for some versions of the db to retrieve the functions.
- js.MetaData.CreateIndex(new Document().Add("_id", 1), true);
- }
-
- public Document this[ String name ] {
- get{
- return GetFunction(name);
- }
- set{
- Add(value);
- }
- }
-
- /// <summary>
- /// Gets the document representing the function in the database.
- /// </summary>
- /// <param name="name">
- /// A <see cref="System.String"/>
- /// </param>
- /// <returns>
- /// A <see cref="Document"/>
- /// </returns>
+ js.MetaData.CreateIndex(new Document().Add("_id", 1), true);
+ }
+
+ public Document this[ String name ] {
+ get{
+ return GetFunction(name);
+ }
+ set{
+ Add(value);
+ }
+ }
+
+ /// <summary>
+ /// Gets the document representing the function in the database.
+ /// </summary>
+ /// <param name="name">
+ /// A <see cref="System.String"/>
+ /// </param>
+ /// <returns>
+ /// A <see cref="Document"/>
+ /// </returns>
public Document GetFunction(string name){
- return js.FindOne(new Document().Add("_id", name));
- }
-
- /// <summary>
- /// Returns a listing of the names of all the functions in the database
- /// </summary>
- public List<string> GetFunctionNames(){
- List<string> l = new List<string>();
- foreach(Document d in js.FindAll().Documents){
- l.Add((String)d["_id"]);
- }
- return l;
- }
-
- public void Add (string name, string func){
- Add(name, new Code(func));
- }
-
+ return js.FindOne(new Document().Add("_id", name));
+ }
+
+ /// <summary>
+ /// Returns a listing of the names of all the functions in the database
+ /// </summary>
+ public List<string> GetFunctionNames(){
+ List<string> l = new List<string>();
+ foreach(Document d in js.FindAll().Documents){
+ l.Add((String)d["_id"]);
+ }
+ return l;
+ }
+
+ public void Add (string name, string func){
+ Add(name, new Code(func));
+ }
+
public void Add (string name, Code func){
- Add(new Document().Add("_id", name).Add("value", func));
- }
-
- /// <summary>
- /// Store a function in the database with an extended attribute called version.
- /// </summary>
- /// <remarks>Version attributes are an extension to the spec. Function names must be unique
- /// to the database so only one version can be stored at a time. This is most useful for libraries
- /// that store function in the database to make sure that the function they are using is the most
- /// up to date.
- /// </remarks>
+ Add(new Document().Add("_id", name).Add("value", func));
+ }
+
+ /// <summary>
+ /// Store a function in the database with an extended attribute called version.
+ /// </summary>
+ /// <remarks>Version attributes are an extension to the spec. Function names must be unique
+ /// to the database so only one version can be stored at a time. This is most useful for libraries
+ /// that store function in the database to make sure that the function they are using is the most
+ /// up to date.
+ /// </remarks>
public void Add(string name, Code func, float version){
- Add(new Document().Add("_id", name).Add("value", func).Add("version", version));
- }
-
- /// <summary>
- /// Stores a function in the database.
- /// </summary>
+ Add(new Document().Add("_id", name).Add("value", func).Add("version", version));
+ }
+
+ /// <summary>
+ /// Stores a function in the database.
+ /// </summary>
public void Add (Document item){
- if(js.FindOne(new Document().Add("_id", item["_id"])) != null){
- throw new ArgumentException(String.Format("Function {0} already exists in the database.", item["_id"]));
- }
- js.Insert(item);
- }
-
- /// <summary>
- /// Removes every function in the database.
- /// </summary>
- public void Clear (){
- js.Delete(new Document());
- }
-
- public bool Contains (Document item){
- return Contains((string)item["_id"]);
- }
-
- /// <summary>
- /// Checks to see if a function named name is stored in the database.
- /// </summary>
- /// <param name="name">
- /// A <see cref="System.String"/>
- /// </param>
- /// <returns>
- /// A <see cref="System.Boolean"/>
- /// </returns>
- public bool Contains (string name){
- return GetFunction(name) != null;
- }
-
- /// <summary>
- /// Copies the functions from the database ordered by _id (name) to the array starting at the index.
- /// </summary>
- /// <param name="array">
- /// A <see cref="Document[]"/> array to coppy to
- /// </param>
- /// <param name="arrayIndex">
- /// A <see cref="System.Int32"/>
- /// </param>
+ if(js.FindOne(new Document().Add("_id", item["_id"])) != null){
+ throw new ArgumentException(String.Format("Function {0} already exists in the database.", item["_id"]));
+ }
+ js.Insert(item);
+ }
+
+ /// <summary>
+ /// Removes every function in the database.
+ /// </summary>
+ public void Clear (){
+ js.Delete(new Document());
+ }
+
+ public bool Contains (Document item){
+ return Contains((string)item["_id"]);
+ }
+
+ /// <summary>
+ /// Checks to see if a function named name is stored in the database.
+ /// </summary>
+ /// <param name="name">
+ /// A <see cref="System.String"/>
+ /// </param>
+ /// <returns>
+ /// A <see cref="System.Boolean"/>
+ /// </returns>
+ public bool Contains (string name){
+ return GetFunction(name) != null;
+ }
+
+ /// <summary>
+ /// Copies the functions from the database ordered by _id (name) to the array starting at the index.
+ /// </summary>
+ /// <param name="array">
+ /// A <see cref="Document[]"/> array to coppy to
+ /// </param>
+ /// <param name="arrayIndex">
+ /// A <see cref="System.Int32"/>
+ /// </param>
public void CopyTo (Document[] array, int arrayIndex){
- Document query = new Document().Add("$orderby", new Document().Add("_id", 1));
- int idx = arrayIndex;
- foreach(Document doc in js.Find(query,array.Length - 1,arrayIndex).Documents){
- array[idx] = doc;
- idx++;
- }
- }
-
- public void Update(Document item){
- throw new System.NotImplementedException();
- }
-
- public bool Remove (Document item){
- return Remove((string)item["_id"]);
- }
-
+ Document query = new Document().Add("$orderby", new Document().Add("_id", 1));
+ int idx = arrayIndex;
+ foreach(Document doc in js.Find(query,array.Length - 1,arrayIndex).Documents){
+ array[idx] = doc;
+ idx++;
+ }
+ }
+
+ public void Update(Document item){
+ throw new System.NotImplementedException();
+ }
+
+ public bool Remove (Document item){
+ return Remove((string)item["_id"]);
+ }
+
public bool Remove (string name){
- js.Delete(new Document().Add("_id", name));
- return true;
- }
-
- public int Count {
- get {
- long cnt = js.Count();
- if(cnt > int.MaxValue) return int.MaxValue; //lots of functions.
- return (int)cnt;
- }
- }
-
- public bool IsReadOnly {
- get {
- return false;
- }
+ js.Delete(new Document().Add("_id", name));
+ return true;
+ }
+
+ public int Count {
+ get {
+ long cnt = js.Count();
+ if(cnt > int.MaxValue) return int.MaxValue; //lots of functions.
+ return (int)cnt;
+ }
+ }
+
+ public bool IsReadOnly {
+ get {
+ return false;
+ }
}
- public IEnumerator<Document> GetEnumerator (){
- foreach(Document doc in js.FindAll().Documents){
- yield return doc;
- }
- yield break;
+ public IEnumerator<Document> GetEnumerator (){
+ foreach(Document doc in js.FindAll().Documents){
+ yield return doc;
+ }
+ yield break;
}
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator (){
- return GetEnumerator();
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator (){
+ return GetEnumerator();
}
- }
-}
+ }
+}
View
120 MongoDBDriver/DatabaseMetaData.cs
@@ -2,80 +2,80 @@
using MongoDB.Driver.Connections;
using MongoDB.Driver.Serialization;
-namespace MongoDB.Driver
-{
- /// <summary>
- /// Administration of metadata for a database.
- /// </summary>
- public class DatabaseMetaData
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Administration of metadata for a database.
+ /// </summary>
+ public class DatabaseMetaData
{
- private ISerializationFactory serializationFactory;
- private Connection connection;
- private string name;
- private Database db;
-
+ private ISerializationFactory serializationFactory;
+ private Connection connection;
+ private string name;
+ private MongoDatabase db;
+
public DatabaseMetaData(ISerializationFactory serializationFactory, string name, Connection conn){
- this.serializationFactory = serializationFactory;
- this.connection = conn;
- this.name = name;
- this.db = new Database(serializationFactory, conn, name);
- }
-
- public IMongoCollection CreateCollection(String name){
- return this.CreateCollection(name,null);
+ this.serializationFactory = serializationFactory;
+ this.connection = conn;
+ this.name = name;
+ this.db = new MongoDatabase(serializationFactory, conn, name);
+ }
+
+ public IMongoCollection CreateCollection(String name){
+ return this.CreateCollection(name,null);
}
public IMongoCollection CreateCollection(String name, Document options)
- {
+ {
Document cmd = new Document();
- cmd.Add("create", name).Merge(options);
+ cmd.Add("create", name).Merge(options);
db.SendCommand(cmd);
- return new Collection(serializationFactory, connection, this.name, name);
+ return new MongoCollection(serializationFactory, connection, this.name, name);
}
- public Boolean DropCollection(Collection col)
- {
- return this.DropCollection(col.Name);
- }
-
+ public Boolean DropCollection(MongoCollection col)
+ {
+ return this.DropCollection(col.Name);
+ }
+
public Boolean DropCollection(String name){
- Document result = db.SendCommand(new Document().Add("drop", name));
- return result.Contains("ok") && ((double)result["ok"] == 1);
- }
-
- public Boolean DropDatabase(){
- Document result = db.SendCommand("dropDatabase");
- return result.Contains("ok") && ((double)result["ok"] == 1);
- }
-
- public void AddUser(string username, string password){
- IMongoCollection users = db["system.users"];
- string pwd = Database.Hash(username + ":mongo:" + password);
- Document user = new Document().Add("user", username).Add("pwd", pwd);
-
- if (FindUser(username) != null){
- throw new MongoException("A user with the name " + username + " already exists in this database.", null);
+ Document result = db.SendCommand(new Document().Add("drop", name));
+ return result.Contains("ok") && ((double)result["ok"] == 1);
+ }
+
+ public Boolean DropDatabase(){
+ Document result = db.SendCommand("dropDatabase");
+ return result.Contains("ok") && ((double)result["ok"] == 1);
+ }
+
+ public void AddUser(string username, string password){
+ IMongoCollection users = db["system.users"];
+ string pwd = MongoDatabase.Hash(username + ":mongo:" + password);
+ Document user = new Document().Add("user", username).Add("pwd", pwd);
+
+ if (FindUser(username) != null){
+ throw new MongoException("A user with the name " + username + " already exists in this database.", null);
}
users.Insert(user);
- }
-
+ }
+
public void RemoveUser(string username){
IMongoCollection users = db["system.users"];
- users.Delete(new Document().Add("user", username));
- }
-
+ users.Delete(new Document().Add("user", username));
+ }
+
public ICursor ListUsers(){
- IMongoCollection users = db["system.users"];
- return users.FindAll();
- }
-
+ IMongoCollection users = db["system.users"];
+ return users.FindAll();
+ }
+
public Document FindUser(string username){
- return FindUser(new Document().Add("user", username));
- }
-
- public Document FindUser(Document spec){
- return db["system.users"].FindOne(spec);
- }
- }
-}
+ return FindUser(new Document().Add("user", username));
+ }
+
+ public Document FindUser(Document spec){
+ return db["system.users"].FindOne(spec);
+ }
+ }
+}
View
2  MongoDBDriver/IMongoDatabase.cs
@@ -93,7 +93,7 @@ public interface IMongoDatabase
/// </summary>
/// <param name="sisterDatabaseName">Name of the sister database.</param>
/// <returns></returns>
- Database GetSisterDatabase(string sisterDatabaseName);
+ MongoDatabase GetSisterDatabase(string sisterDatabaseName);
/// <summary>
/// Resets last error. This is good to call before a bulk operation.
View
206 MongoDBDriver/Mongo.cs
@@ -1,103 +1,103 @@
-using System;
-using MongoDB.Driver.Connections;
-using MongoDB.Driver.Serialization;
-
-namespace MongoDB.Driver
-{
- /// <summary>
- /// Description of Mongo.
- /// </summary>
- public class Mongo : IDisposable
- {
- private readonly Connection connection;
- private readonly ISerializationFactory serializationFactory;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Mongo"/> class.
- /// </summary>
- public Mongo ()
- : this(string.Empty, SerializationFactory.Default)
- { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Mongo"/> class.
- /// </summary>
- /// <param name="serializationFactory">The serialization factory.</param>
- public Mongo(ISerializationFactory serializationFactory)
- : this(string.Empty, serializationFactory)
- { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Mongo"/> class.
- /// </summary>
- /// <param name="connectionString">The connection string.</param>
- public Mongo(string connectionString)
- : this(connectionString, SerializationFactory.Default)
- { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Mongo"/> class.
- /// </summary>
- /// <param name="connectionString">The connection string.</param>
- /// <param name="serializationFactory">The serialization factory.</param>
- public Mongo (string connectionString, ISerializationFactory serializationFactory)
- {
- if(connectionString == null)
- throw new ArgumentNullException("connectionString");
- if(serializationFactory == null)
- throw new ArgumentNullException("serializationFactory");
- this.connection = ConnectionFactory.GetConnection(connectionString);
- this.serializationFactory = serializationFactory;
- }
-
- /// <summary>
- /// Gets the connection string.
- /// </summary>
- /// <value>The connection string.</value>
- public string ConnectionString {
- get { return connection.ConnectionString; }
- }
-
- /// <summary>
- /// Gets the named database.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns></returns>
- public Database GetDatabase (String name){
- return new Database (serializationFactory, connection, name);
- }
-
- /// <summary>
- /// Gets the <see cref="MongoDatabase"/> with the specified name.
- /// </summary>
- /// <value></value>
- public Database this[String name] {
- get { return this.GetDatabase (name); }
- }
-
- /// <summary>
- /// Connects this instance.
- /// </summary>
- /// <returns></returns>
- public Boolean Connect (){
- connection.Open ();
- return connection.State == ConnectionState.Opened;
- }
-
- /// <summary>
- /// Disconnects this instance.
- /// </summary>
- /// <returns></returns>
- public Boolean Disconnect (){
- connection.Close ();
- return connection.State == ConnectionState.Closed;
- }
-
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- public void Dispose (){
- connection.Dispose ();
- }
- }
-}
+using System;
+using MongoDB.Driver.Connections;
+using MongoDB.Driver.Serialization;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ /// Description of Mongo.
+ /// </summary>
+ public class Mongo : IDisposable
+ {
+ private readonly Connection connection;
+ private readonly ISerializationFactory serializationFactory;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Mongo"/> class.
+ /// </summary>
+ public Mongo ()
+ : this(string.Empty, SerializationFactory.Default)
+ { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Mongo"/> class.
+ /// </summary>
+ /// <param name="serializationFactory">The serialization factory.</param>
+ public Mongo(ISerializationFactory serializationFactory)
+ : this(string.Empty, serializationFactory)
+ { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Mongo"/> class.
+ /// </summary>
+ /// <param name="connectionString">The connection string.</param>
+ public Mongo(string connectionString)
+ : this(connectionString, SerializationFactory.Default)
+ { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Mongo"/> class.
+ /// </summary>
+ /// <param name="connectionString">The connection string.</param>
+ /// <param name="serializationFactory">The serialization factory.</param>
+ public Mongo (string connectionString, ISerializationFactory serializationFactory)
+ {
+ if(connectionString == null)
+ throw new ArgumentNullException("connectionString");
+ if(serializationFactory == null)
+ throw new ArgumentNullException("serializationFactory");
+ this.connection = ConnectionFactory.GetConnection(connectionString);
+ this.serializationFactory = serializationFactory;
+ }
+
+ /// <summary>
+ /// Gets the connection string.
+ /// </summary>
+ /// <value>The connection string.</value>
+ public string ConnectionString {
+ get { return connection.ConnectionString; }
+ }
+
+ /// <summary>
+ /// Gets the named database.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ /// <returns></returns>
+ public MongoDatabase GetDatabase (String name){
+ return new MongoDatabase (serializationFactory, connection, name);
+ }
+
+ /// <summary>
+ /// Gets the <see cref="MongoDatabase"/> with the specified name.
+ /// </summary>
+ /// <value></value>
+ public MongoDatabase this[String name] {
+ get { return this.GetDatabase (name); }
+ }
+
+ /// <summary>
+ /// Connects this instance.
+ /// </summary>
+ /// <returns></returns>
+ public Boolean Connect (){
+ connection.Open ();
+ return connection.State == ConnectionState.Opened;
+ }
+
+ /// <summary>
+ /// Disconnects this instance.
+ /// </summary>
+ /// <returns></returns>
+ public Boolean Disconnect (){
+ connection.Close ();
+ return connection.State == ConnectionState.Closed;
+ }
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose (){
+ connection.Dispose ();
+ }
+ }
+}
View
8 MongoDBDriver/Collection.cs → MongoDBDriver/MongoCollection.cs
@@ -11,9 +11,9 @@ namespace MongoDB.Driver
/// <summary>
/// <see cref = "IMongoCollection" />
/// </summary>
- public class Collection : IMongoCollection
+ public class MongoCollection : IMongoCollection
{
- Collection<Document> _collection;
+ MongoCollection<Document> _collection;
public string Database {
get { return _collection.DatabaseName; }
@@ -43,9 +43,9 @@ public class Collection : IMongoCollection
/// <param name="connection">The connection.</param>
/// <param name="databaseName">Name of the database.</param>
/// <param name="name">The name.</param>
- public Collection(ISerializationFactory serializationFactory, Connection connection, string databaseName, string name)
+ public MongoCollection(ISerializationFactory serializationFactory, Connection connection, string databaseName, string name)
{
- _collection = new Collection<Document>(serializationFactory, connection, databaseName, name);
+ _collection = new MongoCollection<Document>(serializationFactory, connection, databaseName, name);
}
public Document FindOne(Document spec){
View
1,401 MongoDBDriver/Collection`1.cs → MongoDBDriver/MongoCollection`1.cs
@@ -1,708 +1,695 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using MongoDB.Driver.Connections;
-using MongoDB.Driver.Protocol;
-using MongoDB.Driver.Serialization;
-
-namespace MongoDB.Driver
-{
- /// <summary>
- ///
- /// </summary>
- public class Collection<T> : IMongoCollection<T> where T : class
- {
- private readonly Connection _connection;
- private Database _database;
- private CollectionMetaData _metaData;
- private readonly ISerializationFactory _serializationFactory;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="MongoCollection&lt;T&gt;"/> class.
- /// </summary>
- /// <param name="serializationFactory">The serialization factory.</param>
- /// <param name="connection">The connection.</param>
- /// <param name="databaseName">Name of the database.</param>
- /// <param name="name">The name.</param>
- public Collection(ISerializationFactory serializationFactory, Connection connection, string databaseName, string name)
- {
- //Todo: This should be internal
- Name = name;
- DatabaseName = databaseName;
- _connection = connection;
- _serializationFactory = serializationFactory;
- }
-
- /// <summary>
- /// Gets the database.
- /// </summary>
- /// <value>The database.</value>
- public IMongoDatabase Database {
- get { return _database ?? (_database = new Database(_serializationFactory, _connection, DatabaseName)); }
- }
-
- /// <summary>
- /// Gets or sets the name.
- /// </summary>
- /// <value>The name.</value>
- public string Name { get; private set; }
-
- /// <summary>
- /// Gets or sets the name of the database.
- /// </summary>
- /// <value>The name of the database.</value>
- public string DatabaseName { get; private set; }
-
- /// <summary>
- /// Gets the full name including database name.
- /// </summary>
- /// <value>The full name.</value>
- public string FullName {
- get { return DatabaseName + "." + Name; }
- }
-
- /// <summary>
- /// Gets the meta data.
- /// </summary>
- /// <value>The meta data.</value>
- public CollectionMetaData MetaData {
- get { return _metaData ?? (_metaData = new CollectionMetaData(_serializationFactory, DatabaseName, Name, _connection)); }
- }
-
- /// <summary>
- /// Finds and returns the first document in a query.
- /// </summary>
- /// <param name="spec">A <see cref="Document"/> representing the query.</param>
- /// <returns>
- /// A <see cref="Document"/> from the collection.
- /// </returns>
- public T FindOne(Document spec){
- return FindOne((object)spec);
- }
-
- /// <summary>
- /// Finds and returns the first document in a query.
- /// </summary>
- /// <param name="spec">A <see cref="Document"/> representing the query.</param>
- /// <returns>
- /// A <see cref="Document"/> from the collection.
- /// </returns>
- public T FindOne(object spec){
- var cursor = Find(spec, -1, 0, null);
- foreach (var document in cursor.Documents) {
- cursor.Dispose();
- return document;
- }
- //FIXME Decide if this should throw a not found exception instead of returning null.
- return null;
- //this.Find(spec, -1, 0, null)[0];
- }
-
- /// <summary>
- /// Finds all.
- /// </summary>
- /// <returns></returns>
- public ICursor<T> FindAll(){
- var spec = new Document();
- return Find(spec, 0, 0, null);
- }
-
- /// <summary>
- /// Finds the specified where.
- /// </summary>
- /// <param name="where">The where.</param>
- /// <returns></returns>
- public ICursor<T> Find(String @where){
- var spec = new Document { { "$where", new Code(@where) } };
- return Find(spec, 0, 0, null);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <returns></returns>
- public ICursor<T> Find(Document spec){
- return Find((object)spec);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <returns></returns>
- public ICursor<T> Find(object spec){
- return Find(spec, 0, 0, null);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="fields"></param>
- /// <returns>A <see cref="ICursor"/></returns>
- public ICursor<T> Find(Document spec, Document fields){
- return Find(spec, 0, 0, fields);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="fields"></param>
- /// <returns>A <see cref="ICursor"/></returns>
- public ICursor<T> Find(object spec, object fields){
- return Find(spec, 0, 0, fields);
- }
-
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="limit">The limit.</param>
- /// <param name="skip">The skip.</param>
- /// <returns></returns>
- public ICursor<T> Find(Document spec, int limit, int skip){
- return Find((object)spec, limit, skip, null);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="limit">The limit.</param>
- /// <param name="skip">The skip.</param>
- /// <returns></returns>
- public ICursor<T> Find(object spec, int limit, int skip){
- return Find(spec, limit, skip, null);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="limit">The limit.</param>
- /// <param name="skip">The skip.</param>
- /// <param name="fields">The fields.</param>
- /// <returns></returns>
- public ICursor<T> Find(Document spec, int limit, int skip, Document fields){
- return Find((object)spec, limit, skip, (object)fields);
- }
-
- /// <summary>
- /// Finds the specified spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <param name="limit">The limit.</param>
- /// <param name="skip">The skip.</param>
- /// <param name="fields">The fields.</param>
- /// <returns></returns>
- public ICursor<T> Find(object spec, int limit, int skip, object fields){
- if (spec == null)
- spec = new Document();
- return new Cursor<T>(_serializationFactory, _connection, FullName, spec, limit, skip, fields);
- }
-
- /// <summary>
- /// Entrypoint into executing a map/reduce query against the collection.
- /// </summary>
- /// <returns>A <see cref="MapReduce"/></returns>
- public MapReduce MapReduce(){
- return new MapReduce(Database, Name);
- }
-
- /// <summary>
- /// Maps the reduce builder.
- /// </summary>
- /// <returns></returns>
- public MapReduceBuilder MapReduceBuilder(){
- return new MapReduceBuilder(MapReduce());
- }
-
- ///<summary>
- /// Count all items in the collection.
- ///</summary>
- public long Count(){
- return Count(new Document());
- }
-
- /// <summary>
- /// Count all items in a collection that match the query spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <returns></returns>
- /// <remarks>
- /// It will return 0 if the collection doesn't exist yet.
- /// </remarks>
- public long Count(Document spec){
- return Count((object)spec);
- }
-
- /// <summary>
- /// Count all items in a collection that match the query spec.
- /// </summary>
- /// <param name="spec">The spec.</param>
- /// <returns></returns>
- /// <remarks>
- /// It will return 0 if the collection doesn't exist yet.
- /// </remarks>
- public long Count(object spec){
- try {
- var response = Database.SendCommand(new Document().Add("count", Name).Add("query", spec));
- return Convert.ToInt64((double)response["n"]);
- } catch (MongoCommandException) {
- //FIXME This is an exception condition when the namespace is missing.
- //-1 might be better here but the console returns 0.
- return 0;
- }
- }
-
- /// <summary>
- /// Inserts the Document into the collection.
- /// </summary>
- public void Insert(Document document, bool safemode){
- Insert((object)document, safemode);
- }
-
- /// <summary>
- /// Inserts the Document into the collection.
- /// </summary>
- public void Insert(object document, bool safemode){
- Insert(document);
- CheckError(safemode);
- }
-
- /// <summary>
- /// Inserts the specified doc.
- /// </summary>
- /// <param name="document">The doc.</param>
- public void Insert(Document document){
- Insert((object)document);
- }
-
- /// <summary>
- /// Inserts the specified doc.
- /// </summary>
- /// <param name="document">The doc.</param>
- public void Insert(object document){
- Insert(new[] { document });
- }
-
- /// <summary>
- /// Inserts the specified documents.
- /// </summary>
- /// <param name="documents">The documents.</param>
- /// <param name="safemode">if set to <c>true</c> [safemode].</param>
- public void Insert(IEnumerable<Document> documents, bool safemode){
- Insert((IEnumerable<object>)documents, safemode);
- }
-
- /// <summary>
- /// Inserts the specified documents.
- /// </summary>
- /// <param name="documents">The documents.</param>
- /// <param name="safemode">if set to <c>true</c> [safemode].</param>
- public void Insert<TElement>(IEnumerable<TElement> documents, bool safemode){
- if (safemode)
- Database.ResetError();
- Insert(documents);
- CheckPreviousError(safemode);
- }
-
- /// <summary>
- /// Inserts the specified documents.
- /// </summary>
- /// <param name="documents">The documents.</param>
- public void Insert(IEnumerable<Document> documents){
- Insert<Document>(documents);
- }
-
- /// <summary>
- /// Inserts the specified documents.
- /// </summary>
- /// <param name="documents">The documents.</param>
- public void Insert<TElement>(IEnumerable<TElement> documents){
- var rootType = typeof(T);
- var bsonDescriptor = _serializationFactory.GetBsonDescriptor(rootType, _connection);
-
- var insertMessage = new InsertMessage(bsonDescriptor)
- {
- FullCollectionName = FullName
- };
-
- var descriptor = _serializationFactory.GetObjectDescriptor(rootType);
- var insertDocument = new List<object>();
-
- foreach (var document in documents) {
- var id = descriptor.GetPropertyValue(document, "_id");
-
- if (id == null)
- descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(document));
-
- insertDocument.Add(document);
- }
-
- insertMessage.Documents = insertDocument.ToArray();
-
- try {
- _connection.SendMessage(insertMessage);
- } catch (IOException exception) {
- throw new MongoCommException("Could not insert document, communication failure", _connection, exception);
- }
- }
-
- /// <summary>
- /// Deletes documents from the collection according to the spec.
- /// </summary>
- /// <param name="selector">The selector.</param>
- /// <param name="safemode">if set to <c>true</c> [safemode].</param>
- /// <remarks>
- /// An empty document will match all documents in the collection and effectively truncate it.
- /// </remarks>
- public void Delete(Document selector, bool safemode){
- Delete((object)selector, safemode);
- }
-
- /// <summary>
- /// Deletes documents from the collection according to the spec.
- /// </summary>
- /// <param name="selector">The selector.</param>
- /// <param name="safemode">if set to <c>true</c> [safemode].</param>
- /// <remarks>
- /// An empty document will match all documents in the collection and effectively truncate it.
- /// </remarks>
- public void Delete(object selector, bool safemode){
- Delete(selector);
- CheckError(safemode);
- }
-
- /// <summary>
- /// Deletes documents from the collection according to the spec.
- /// </summary>
- /// <param name="selector">The selector.</param>
- /// <remarks>
- /// An empty document will match all documents in the collection and effectively truncate it.
- /// </remarks>
- public void Delete(Document selector){
- Delete((object)selector);
- }
-
- /// <summary>
- /// Deletes documents from the collection according to the spec.
- /// </summary>
- /// <param name="selector">The selector.</param>
- /// <remarks>
- /// An empty document will match all documents in the collection and effectively truncate it.
- /// </remarks>
- public void Delete(object selector){
- var descriptor = _serializationFactory.GetBsonDescriptor(typeof(T), _connection);
-
- var deleteMessage = new DeleteMessage(descriptor) { FullCollectionName = FullName, Selector = selector };
-
- try {
- _connection.SendMessage(deleteMessage);
- } catch (IOException exception) {
- throw new MongoCommException("Could not delete document, communication failure", _connection, exception);
- }
- }
-
- /// <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(Document document, bool safemode){
- Update((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);
- }
-
- /// <summary>
- /// Updates a document with the data in doc as found by the selector.
- /// </summary>
- /// <param name="document">The document.</param>
- /// <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 document){
- Update((object)document);
- }
-
- /// <summary>
- /// Updates a document with the data in doc as found by the selector.
- /// </summary>
- /// <param name="document">The document.</param>
- /// <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(object document){
- //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 descriptor = _serializationFactory.GetObjectDescriptor(typeof(T));
-
- var value = descriptor.GetPropertyValue(document, "_id");
-
- if (value != null) {
- selector["_id"] = value;
- } else {
+using System;
+using System.Collections.Generic;
+using System.IO;
+using MongoDB.Driver.Connections;
+using MongoDB.Driver.Protocol;
+using MongoDB.Driver.Serialization;
+
+namespace MongoDB.Driver
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class MongoCollection<T> : IMongoCollection<T> where T : class
+ {
+ private readonly Connection _connection;
+ private MongoDatabase _database;
+ private CollectionMetaData _metaData;
+ private readonly ISerializationFactory _serializationFactory;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MongoCollection&lt;T&gt;"/> class.
+ /// </summary>
+ /// <param name="serializationFactory">The serialization factory.</param>
+ /// <param name="connection">The connection.</param>
+ /// <param name="databaseName">Name of the database.</param>
+ /// <param name="name">The name.</param>
+ public MongoCollection(ISerializationFactory serializationFactory, Connection connection, string databaseName, string name)
+ {
+ //Todo: This should be internal
+ Name = name;
+ DatabaseName = databaseName;
+ _connection = connection;
+ _serializationFactory = serializationFactory;
+ }
+
+ /// <summary>
+ /// Gets the database.
+ /// </summary>
+ /// <value>The database.</value>
+ public IMongoDatabase Database {
+ get { return _database ?? (_database = new MongoDatabase(_serializationFactory, _connection, DatabaseName)); }
+ }
+
+ /// <summary>
+ /// Gets or sets the name.
+ /// </summary>
+ /// <value>The name.</value>
+ public string Name { get; private set; }
+
+ /// <summary>
+ /// Gets or sets the name of the database.
+ /// </summary>
+ /// <value>The name of the database.</value>
+ public string DatabaseName { get; private set; }
+
+ /// <summary>
+ /// Gets the full name including database name.
+ /// </summary>
+ /// <value>The full name.</value>
+ public string FullName {
+ get { return DatabaseName + "." + Name; }
+ }
+
+ /// <summary>
+ /// Gets the meta data.
+ /// </summary>
+ /// <value>The meta data.</value>
+ public CollectionMetaData MetaData {
+ get { return _metaData ?? (_metaData = new CollectionMetaData(_serializationFactory, DatabaseName, Name, _connection)); }
+ }
+
+ /// <summary>
+ /// Finds and returns the first document in a query.
+ /// </summary>
+ /// <param name="spec">A <see cref="Document"/> representing the query.</param>
+ /// <returns>
+ /// A <see cref="Document"/> from the collection.
+ /// </returns>
+ public T FindOne(Document spec){
+ return FindOne((object)spec);
+ }
+
+ /// <summary>
+ /// Finds and returns the first document in a query.
+ /// </summary>
+ /// <param name="spec">A <see cref="Document"/> representing the query.</param>
+ /// <returns>
+ /// A <see cref="Document"/> from the collection.
+ /// </returns>
+ public T FindOne(object spec){
+ var cursor = Find(spec, -1, 0, null);
+ foreach (var document in cursor.Documents) {
+ cursor.Dispose();
+ return document;
+ }
+ //FIXME Decide if this should throw a not found exception instead of returning null.
+ return null;
+ //this.Find(spec, -1, 0, null)[0];
+ }
+
+ /// <summary>
+ /// Finds all.
+ /// </summary>
+ /// <returns></returns>
+ public ICursor<T> FindAll(){
+ var spec = new Document();
+ return Find(spec, 0, 0, null);
+ }
+
+ /// <summary>
+ /// Finds the specified where.
+ /// </summary>
+ /// <param name="where">The where.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(String @where){
+ var spec = new Document { { "$where", new Code(@where) } };
+ return Find(spec, 0, 0, null);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(Document spec){
+ return Find((object)spec);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(object spec){
+ return Find(spec, 0, 0, null);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="fields"></param>
+ /// <returns>A <see cref="ICursor"/></returns>
+ public ICursor<T> Find(Document spec, Document fields){
+ return Find(spec, 0, 0, fields);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="fields"></param>
+ /// <returns>A <see cref="ICursor"/></returns>
+ public ICursor<T> Find(object spec, object fields){
+ return Find(spec, 0, 0, fields);
+ }
+
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="limit">The limit.</param>
+ /// <param name="skip">The skip.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(Document spec, int limit, int skip){
+ return Find((object)spec, limit, skip, null);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="limit">The limit.</param>
+ /// <param name="skip">The skip.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(object spec, int limit, int skip){
+ return Find(spec, limit, skip, null);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="limit">The limit.</param>
+ /// <param name="skip">The skip.</param>
+ /// <param name="fields">The fields.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(Document spec, int limit, int skip, Document fields){
+ return Find((object)spec, limit, skip, (object)fields);
+ }
+
+ /// <summary>
+ /// Finds the specified spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <param name="limit">The limit.</param>
+ /// <param name="skip">The skip.</param>
+ /// <param name="fields">The fields.</param>
+ /// <returns></returns>
+ public ICursor<T> Find(object spec, int limit, int skip, object fields){
+ if (spec == null)
+ spec = new Document();
+ return new Cursor<T>(_serializationFactory, _connection, FullName, spec, limit, skip, fields);
+ }
+
+ /// <summary>
+ /// Entrypoint into executing a map/reduce query against the collection.
+ /// </summary>
+ /// <returns>A <see cref="MapReduce"/></returns>
+ public MapReduce MapReduce(){
+ return new MapReduce(Database, Name);
+ }
+
+ /// <summary>
+ /// Maps the reduce builder.
+ /// </summary>
+ /// <returns></returns>
+ public MapReduceBuilder MapReduceBuilder(){
+ return new MapReduceBuilder(MapReduce());
+ }
+
+ ///<summary>
+ /// Count all items in the collection.
+ ///</summary>
+ public long Count(){
+ return Count(new Document());
+ }
+
+ /// <summary>
+ /// Count all items in a collection that match the query spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// It will return 0 if the collection doesn't exist yet.
+ /// </remarks>
+ public long Count(Document spec){
+ return Count((object)spec);
+ }
+
+ /// <summary>
+ /// Count all items in a collection that match the query spec.
+ /// </summary>
+ /// <param name="spec">The spec.</param>
+ /// <returns></returns>
+ /// <remarks>
+ /// It will return 0 if the collection doesn't exist yet.
+ /// </remarks>
+ public long Count(object spec){
+ try {
+ var response = Database.SendCommand(new Document().Add("count", Name).Add("query", spec));
+ return Convert.ToInt64((double)response["n"]);
+ } catch (MongoCommandException) {
+ //FIXME This is an exception condition when the namespace is missing.
+ //-1 might be better here but the console returns 0.
+ return 0;
+ }
+ }
+
+ /// <summary>
+ /// Inserts the Document into the collection.
+ /// </summary>
+ public void Insert(Document document, bool safemode){
+ Insert((object)document, safemode);
+ }
+
+ /// <summary>
+ /// Inserts the Document into the collection.
+ /// </summary>
+ public void Insert(object document, bool safemode){
+ Insert(document);
+ CheckError(safemode);
+ }
+
+ /// <summary>
+ /// Inserts the specified doc.
+ /// </summary>
+ /// <param name="document">The doc.</param>
+ public void Insert(Document document){
+ Insert((object)document);
+ }
+
+ /// <summary>
+ /// Inserts the specified doc.
+ /// </summary>
+ /// <param name="document">The doc.</param>
+ public void Insert(object document){
+ Insert(new[] { document });
+ }
+
+ /// <summary>
+ /// Inserts the specified documents.
+ /// </summary>
+ /// <param name="documents">The documents.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Insert(IEnumerable<Document> documents, bool safemode){
+ Insert((IEnumerable<object>)documents, safemode);
+ }
+
+ /// <summary>
+ /// Inserts the specified documents.
+ /// </summary>
+ /// <param name="documents">The documents.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Insert<TElement>(IEnumerable<TElement> documents, bool safemode){
+ if (safemode)
+ Database.ResetError();
+ Insert(documents);
+ CheckPreviousError(safemode);
+ }
+
+ /// <summary>
+ /// Inserts the specified documents.
+ /// </summary>
+ /// <param name="documents">The documents.</param>
+ public void Insert(IEnumerable<Document> documents){
+ Insert<Document>(documents);
+ }
+
+ /// <summary>
+ /// Inserts the specified documents.
+ /// </summary>
+ /// <param name="documents">The documents.</param>
+ public void Insert<TElement>(IEnumerable<TElement> documents){
+ var rootType = typeof(T);
+ var bsonDescriptor = _serializationFactory.GetBsonDescriptor(rootType, _connection);
+
+ var insertMessage = new InsertMessage(bsonDescriptor)
+ {
+ FullCollectionName = FullName
+ };
+
+ var descriptor = _serializationFactory.GetObjectDescriptor(rootType);
+ var insertDocument = new List<object>();
+
+ foreach (var document in documents) {
+ var id = descriptor.GetPropertyValue(document, "_id");
+
+ if (id == null)
+ descriptor.SetPropertyValue(document, "_id", descriptor.GenerateId(document));
+
+ insertDocument.Add(document);
+ }
+
+ insertMessage.Documents = insertDocument.ToArray();
+
+ try {
+ _connection.SendMessage(insertMessage);
+ } catch (IOException exception) {
+ throw new MongoCommException("Could not insert document, communication failure", _connection, exception);
+ }
+ }
+
+ /// <summary>
+ /// Deletes documents from the collection according to the spec.
+ /// </summary>
+ /// <param name="selector">The selector.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ /// <remarks>
+ /// An empty document will match all documents in the collection and effectively truncate it.
+ /// </remarks>
+ public void Delete(Document selector, bool safemode){
+ Delete((object)selector, safemode);
+ }
+
+ /// <summary>
+ /// Deletes documents from the collection according to the spec.
+ /// </summary>
+ /// <param name="selector">The selector.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ /// <remarks>
+ /// An empty document will match all documents in the collection and effectively truncate it.
+ /// </remarks>
+ public void Delete(object selector, bool safemode){
+ Delete(selector);
+ CheckError(safemode);
+ }
+
+ /// <summary>
+ /// Deletes documents from the collection according to the spec.
+ /// </summary>
+ /// <param name="selector">The selector.</param>
+ /// <remarks>
+ /// An empty document will match all documents in the collection and effectively truncate it.
+ /// </remarks>
+ public void Delete(Document selector){
+ Delete((object)selector);
+ }
+
+ /// <summary>
+ /// Deletes documents from the collection according to the spec.
+ /// </summary>
+ /// <param name="selector">The selector.</param>
+ /// <remarks>
+ /// An empty document will match all documents in the collection and effectively truncate it.
+ /// </remarks>
+ public void Delete(object selector){
+ var descriptor = _serializationFactory.GetBsonDescriptor(typeof(T), _connection);
+
+ var deleteMessage = new DeleteMessage(descriptor) { FullCollectionName = FullName, Selector = selector };
+
+ try {
+ _connection.SendMessage(deleteMessage);
+ } catch (IOException exception) {
+ throw new MongoCommException("Could not delete document, communication failure", _connection, exception);
+ }
+ }
+
+ /// <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(Document document, bool safemode){
+ Update((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);
+ }
+
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <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("Use Save(Document)")]
+ public void Update(Document document){
+ Update((object)document);
+ }
+
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <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("Use Save(Document)")]
+ public void Update(object document){
+ Save(document);
+ }
+
+ /// <summary>
+ /// Updates the specified document.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Update(Document document, Document selector, bool safemode){
+ Update((object)document, (object)selector, 0, safemode);
+ //Update((object)document, (object)document, safemode);
+ }
+
+ /// <summary>
+ /// Updates the specified document.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Update(object document, object selector, bool safemode){
+ Update(document, selector, 0, safemode);
+ }
+
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ public void Update(Document document, Document selector){
+ Update((object)document, (object)selector);
+ }
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ public void Update(object document, object selector){
+ Update(document, selector, 0);
+ }
+
+ /// <summary>
+ /// Updates the specified document.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ /// <param name="flags">The flags.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Update(Document document, Document selector, UpdateFlags flags, bool safemode){
+ Update((object)document, (object)selector, flags, safemode);
+ }
+
+ /// <summary>
+ /// Updates the specified document.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>
+ /// <param name="flags">The flags.</param>
+ /// <param name="safemode">if set to <c>true</c> [safemode].</param>
+ public void Update(object document, object selector, UpdateFlags flags, bool safemode){
+ Update(document, selector, flags);
+ CheckError(safemode);
+ }
+
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The <see cref="Document"/> to update with</param>
+ /// <param name="selector">The query spec to find the document to update.</param>
+ /// <param name="flags"><see cref="UpdateFlags"/></param>
+ public void Update(Document document, Document selector, UpdateFlags flags){
+ Update((object)document, (object)selector, flags);
+ }
+ /// <summary>
+ /// Updates a document with the data in doc as found by the selector.
+ /// </summary>
+ /// <param name="document">The <see cref="Document"/> to update with</param>
+ /// <param name="selector">The query spec to find the document to update.</param>
+ /// <param name="flags"><see cref="UpdateFlags"/></param>
+ 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 };
+
+ try {
+ _connection.SendMessage(updateMessage);
+ } catch (IOException exception) {
+ throw new MongoCommException("Could not update document, communication failure", _connection, exception);
+ }
+ }
+
+ /// <summary>
+ /// Runs a multiple update query against the database. It will wrap any
+ /// doc with $set if the passed in doc doesn't contain any '$' ops.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="selector">The selector.</param>