This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add collection.Remove and mare collection.Delete as obsolete because …
…that match more the javascript client.
- Loading branch information
Showing
10 changed files
with
311 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("\"", "")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.