

using System;using System.Configuration;using System.Linq;using MongoDB;using MongoDB.Configuration;using MongoDB.Linq;namespace Simple {
////// 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.///internalclass MainClass {
private IMongoCollection categories;private Mongo mongo;private IMongoDatabase simple;privateclass MyClass {
publicstring Name { get; set; }publicint Corners { get; set; } }
privateclass SubClass : MyClass {
publicdouble Ratio { get; set; } }
publicstaticvoid Main(string[] args) {
#region 以下为被屏蔽源码部分,我们先从SetUp方法开始//var config = new MongoConfigurationBuilder();//// COMMENT OUT FROM HERE//config.Mapping(mapping =>//{// mapping.DefaultProfile(profile =>// {// profile.SubClassesAre(t => t.IsSubclassOf(typeof(MyClass)));// });// mapping.Map();// mapping.Map();//});//// TO HERE//config.ConnectionString("Server=127.0.0.1");//using (Mongo mongo = new Mongo(config.BuildConfiguration()))//{// mongo.Connect();// try// {// var db = mongo.GetDatabase("TestDb");// var collection = db.GetCollection();// MyClass square = new MyClass()// {// Corners = 4,// Name = "Square"// };// MyClass circle = new MyClass()// {// Corners = 0,// Name = "Circle"// };// SubClass sub = new SubClass()// {// Name = "SubClass",// Corners = 6,// Ratio = 3.43// };// collection.Save(square);// collection.Save(circle);// collection.Save(sub);// var superclass = (from item in db.GetCollection("MyClass").Linq()// where item.Corners > 1// select item.Corners).ToList();// var subclass = (from item in db.GetCollection("MyClass").Linq()// where item.Ratio > 1// select item.Corners).ToList();// Console.WriteLine("Count by LINQ on typed collection: {0}", collection.Linq().Count(x => x.Corners > 1));// Console.WriteLine("Count by LINQ on typed collection2: {0}", db.GetCollection().Linq().Count(x => x.Corners > 1));////Console.WriteLine("Count by LINQ on typed collection3: {0}", db.GetCollection().Count(new { Corners = Op.GreaterThan(1) }));// Console.WriteLine("Count on typed collection: {0}", collection.Count(new { Corners = Op.GreaterThan(1) }));// var coll = db.GetCollection("MyClass");// var count = coll.Count(new Document("Corners", Op.GreaterThan(1)));// Console.WriteLine("Count: {0}", count);// Console.ReadKey();// }// finally// {// mongo.Disconnect();// }//}#endregion var main
=new MainClass(); main.Setup();
main.Run();
Console.ReadLine();
}
////// Setup the collection and insert some data into it.///publicvoid Setup() {
//从配置文件读取连接字符串 IP+端口var connstr = ConfigurationManager.AppSettings["simple"];if(String.IsNullOrEmpty(connstr))thrownew ArgumentNullException("Connection string not found.");//创建Mongo数据服务及连接mongo =new Mongo(connstr); mongo.Connect();
//获取数据库实例(如果该实例不存,就会自动创建simple实例)simple = mongo["simple"];//获取表名(如果该表名不存在,就会自动创建categories表名)categories = simple.GetCollection("categories");//添加记录前清除所有记录Clean(); var names
=new[] {"Bluez", "Jazz", "Classical", "Rock", "Oldies", "Heavy Metal"};//循环插入记录 doucment会自动生成键值_id,id的编码体现了数据的插入顺序foreach(var name in names) categories.Insert(
new Document {{"name", name}}); }
publicvoid Clean() {
//删除document name 为Jazz的记录categories.Remove(new Document {{"name", "Jazz"}}); //remove documents with the name Jazz.//删除所有记录集categories.Remove(new Document()); //remove everything from the categories collection.}publicvoid Run() {
//查找单条记录 参数类型为Documentvar category = categories.FindOne(new Document {{"name", "Bluez"}}); Console.WriteLine(
"The>