Permalink
Browse files

Add collection.Remove and mare collection.Delete as obsolete because …

…that match more the javascript client.
  • Loading branch information...
lanwin committed May 6, 2010
1 parent 2f438f1 commit fd139f9de76dc29ed5f7f5e4b231c526d11f2414
View
@@ -1,100 +1,101 @@
using System;
using System.Configuration;
-
using MongoDB;
namespace Simple
{
/// <summary>
- /// Illustrates some simple operations on the database.
- /// Creating a database connection.
- /// Remove some documents.
- /// Insert some documents
- /// Find one document
- /// Find several documents and iterate through them.
+ /// Illustrates some simple operations on the database.
+ /// Creating a database connection.
+ /// Remove some documents.
+ /// Insert some documents
+ /// Find one document
+ /// Find several documents and iterate through them.
/// </summary>
- class MainClass
+ internal class MainClass
{
- Mongo mongo;
- IMongoDatabase simple;
- IMongoCollection categories;
-
- public static void Main (string[] args){
- MainClass main = new MainClass();
+ private IMongoCollection categories;
+ private Mongo mongo;
+ private IMongoDatabase simple;
+
+ public static void Main(string[] args)
+ {
+ var main = new MainClass();
main.Setup();
main.Run();
}
-
/// <summary>
- /// Setup the collection and insert some data into it.
+ /// Setup the collection and insert some data into it.
/// </summary>
- public void Setup(){
- string connstr = ConfigurationManager.AppSettings["simple"];
- if(String.IsNullOrEmpty(connstr)) throw new ArgumentNullException("Connection string not found.");
+ public void Setup()
+ {
+ var connstr = ConfigurationManager.AppSettings["simple"];
+ if(String.IsNullOrEmpty(connstr))
+ throw new ArgumentNullException("Connection string not found.");
mongo = new Mongo(connstr);
mongo.Connect();
simple = mongo["simple"];
categories = simple["categories"];
- Clean();
-
- var names = new string[]{"Bluez", "Jazz", "Classical", "Rock", "Oldies", "Heavy Metal"};
- foreach(string name in names){
- categories.Insert(new Document(){{"name", name}});
- }
+ Clean();
+
+ var names = new[] {"Bluez", "Jazz", "Classical", "Rock", "Oldies", "Heavy Metal"};
+ foreach(var name in names)
+ categories.Insert(new Document {{"name", name}});
}
-
- public void Clean(){
- categories.Delete(new Document(){{"name", "Jazz"}}); //remove documents with the name Jazz.
- categories.Delete(new Document()); //remove everything from the categories collection.
+
+ public void Clean()
+ {
+ categories.Remove(new Document {{"name", "Jazz"}}); //remove documents with the name Jazz.
+ categories.Remove(new Document()); //remove everything from the categories collection.
}
-
- public void Run(){
- var category = categories.FindOne(new Document { { "name", "Bluez" } });
-
- Console.WriteLine ("The id findOne" + category["_id"]);
-
- Document selector = new Document(){{"_id", category["_id"]}};
-
- category["name"] = "Bluess";
+
+ public void Run()
+ {
+ var category = categories.FindOne(new Document {{"name", "Bluez"}});
+
+ Console.WriteLine("The id findOne" + category["_id"]);
+
+ var selector = new Document {{"_id", category["_id"]}};
+
+ category["name"] = "Bluess";
//The following will do the same thing.
categories.Save(category);
-
- Console.WriteLine("Category after one update " + categories.FindOne(selector).ToString());
-
+
+ Console.WriteLine("Category after one update " + categories.FindOne(selector));
+
category["name"] = "Blues";
categories.Update(category, selector);
-
- Console.WriteLine("Category after two updates " + categories.FindOne(selector).ToString());
-
+
+ Console.WriteLine("Category after two updates " + categories.FindOne(selector));
+
//Find it by _id that has been converted to a string now.
- string id = ((Oid)category["_id"]).ToString();
-
+ var id = (category["_id"]).ToString();
+
Console.WriteLine("Found by string id converted back to Oid");
- Console.WriteLine(categories.FindOne(new Document(){{"_id", id.ToOid()}}));
+ Console.WriteLine(categories.FindOne(new Document {{"_id", id.ToOid()}}));
//Find(new Document()) is equivalent to FindAll();
//Specifying the cursor in a using block will close it on the server if we decide not
//to iterate through the whole thing.
- using(ICursor all = categories.Find(new Document()))
+ using(var all = categories.Find(new Document()))
{
- foreach(Document doc in all.Documents){
+ foreach(var doc in all.Documents)
Console.WriteLine(doc.ToString());
- }
}
-
+
mongo.Disconnect();
}
-
}
}
public static class OidExtensions
{
- public static Oid ToOid (this string str){
- if (str.Length == 24)
- return new Oid (str);
-
- return new Oid (str.Replace ("\"", ""));
+ public static Oid ToOid(this string str)
+ {
+ if(str.Length == 24)
+ return new Oid(str);
+
+ return new Oid(str.Replace("\"", ""));
}
-}
+}
@@ -48,8 +48,8 @@ Namespace Simple
End Sub
Public Sub Clean()
- categories.Delete(New Document().Add("name", "Jazz")) 'remove documents with the name Jazz.
- categories.Delete(new Document()) 'remove everything from the categories collection.
+ categories.Remove(New Document().Add("name", "Jazz")) 'remove documents with the name Jazz.
+ categories.Remove(New Document()) 'remove everything from the categories collection.
End Sub
Public Sub Run()
Dim category As Document = categories.FindOne(New Document().Add("name", "Bluez"))
@@ -45,6 +45,7 @@
<RegisterForComInterop>false</RegisterForComInterop>
<DocumentationFile>bin\Debug\MongoDB.GridFS.xml</DocumentationFile>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+ <NoWarn>618</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
@@ -54,7 +54,7 @@ public void Add(Document item)
/// </exception>
public void Clear()
{
- _collection.Delete(new Document());
+ _collection.Remove(new Document());
}
/// <summary>
@@ -257,7 +257,7 @@ public void Update(Document item)
/// <returns></returns>
public bool Remove(string name)
{
- _collection.Delete(new Document().Add("_id", name));
+ _collection.Remove(new Document().Add("_id", name));
return true;
}
}
@@ -106,7 +106,7 @@ public void AddUser(string username, string password)
public void RemoveUser(string username)
{
var users = _database["system.users"];
- users.Delete(new Document().Add("user", username));
+ users.Remove(new Document().Add("user", username));
}
/// <summary>
Oops, something went wrong.

0 comments on commit fd139f9

Please sign in to comment.