宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

上一节我们在samus的simple例子简单的入门了,这一节将要探讨的问题写个简要

  • 对象存储
  • 继续关系对象的存储
  • LINQ体现
  • 类关系分析

一、对象存储

继续在samus源码上分析,依然是simple的例子。Demo中.net 3.5特性基本上都涉及了。

第三节 MongoDB下samus源码初探-风君子博客第三节 MongoDB下samus源码初探-风君子博客View Code public static void Mainstring[] args)
{
#region 以下为Mongo配置及关系映射部分
//var config = new MongoConfigurationBuilder);
//COMMENT OUT FROM HERE
//config.Mappingmapping =>
//{
// mapping.DefaultProfileprofile =>
// {
// profile.SubClassesAret => t.IsSubclassOftypeofMyClass)));
// });
// mapping.Map<MyClass>);
// mapping.Map<SubClass>);
//});
// TO HERE
#endregion

//config.ConnectionString"Server=127.0.0.1");
//using Mongo mongo = new Mongoconfig.BuildConfiguration)))
using Mongo mongo = new Mongo"Server=127.0.0.1"))
{
mongo.Connect);
try
{
//索引器方式mogo["TestDb"]
var db = mongo.GetDatabase"TestDb");
//老版不支持泛型的时候写法是这样的db.GetCollection"Name") ->看源码重构了一个MongoCollection_1类
//将老版方法重构为db.GetCollection<Documnet>"Name");
//此方法其实将类名反射为集合 db.GetCollection<MyClass>"MyClass")
var collection = db.GetCollection<MyClass>);

//.net 3.5集合初始化特性 创建3个对象(2个MyClass,1个子类SubClass)
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.Savesquare);
collection.Savecircle);
collection.Savesub);

#region LINQ写法读取数据
//var superclass = from item in db.GetCollection<MyClass>"MyClass").Linq)
// where item.Corners > 1
// select item.Corners).ToList);

//var subclass = from item in db.GetCollection<SubClass>"MyClass").Linq)
// where item.Ratio > 1
// select item.Corners).ToList);
#endregion

//Lambda写法
//读取集合MyClass所有对象
Console.WriteLine"Count by LINQ on typed collection: {0}", collection.Linq).Countx => x.Corners > 1));
//读取集合为SubClass的对象由于SubClass被保存到MyClass集合中了,故结果为0)
Console.WriteLine"Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>).Linq).Countx => x.Corners > 1));
//Console.WriteLine"Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>).Countnew { Corners = Op.GreaterThan1) }));

//注:op 是Document的子类 虽然我们存的是其它对象,但所有的对象最终都是以Document类型存储的,
//也就意味检索数据也是Document方式查找或统计了,以下Lambda方式的统计就体现了
//在基类中大家可以看到都是countnew Document))方式及条件查询的,这里介绍完了,下面的代码就容易理解了
Console.WriteLine"Count on typed collection: {0}", collection.Countnew { Corners = Op.GreaterThan1) }));
var count
= collection.Countnew Document"Corners", Op.GreaterThan1)));
Console.WriteLine
"Count: {0}", count);
Console.ReadKey);
}
finally
{
mongo.Disconnect);
}
}

//var main = new MainClass);
//main.Setup);
//main.Run);

Console.ReadLine);
}

看一下结果:

第三节 MongoDB下samus源码初探-风君子博客

在打开mongo.exe查看一下结果

第三节 MongoDB下samus源码初探-风君子博客

是不是很奇怪,MyClass,SubClass对象又没有标记为可序列化,如何就被保存下来了?难道是通过反射对实现的,看看源码

第三节 MongoDB下samus源码初探-风君子博客

二、继续关系对象的存储

当数据保存为文档记录时,MongoDB是如何识别这个两类是有继承关系的呢?我们接着下面的代码修改一下继续

先删除上次的记录,在mongo.exe中运行db.MyClass.drop) 删除集合中的数据,查看更多操作集合的命令可执行db.MyClass.help

mongo.exe脚本命令官方介绍用的是Javascript脚本,从命名可以看出是它的影子。mongo.exe的脚本学习可参照这个链接

http://special.csdn.net/mongodb/index.html 

第三节 MongoDB下samus源码初探-风君子博客第三节 MongoDB下samus源码初探-风君子博客View Code public static void Mainstring[] args)
{
#region 以下为Mongo配置及关系映射部分
var config
= new MongoConfigurationBuilder);
//COMMENT OUT FROM HERE 建立两者的关系
config.Mappingmapping =>
{
mapping.DefaultProfileprofile
=>
{
profile.SubClassesAret
=> t.IsSubclassOftypeofMyClass)));
});
mapping.Map
<MyClass>);
mapping.Map
<SubClass>);
});
// TO HERE
#endregion

config.ConnectionString"Server=127.0.0.1");
//将配置注入Mongo类中
using Mongo mongo = new Mongoconfig.BuildConfiguration)))
{
mongo.Connect);
try
{
//索引器方式mogo["TestDb"]
var db = mongo.GetDatabase"TestDb");
//老版不支持泛型的时候写法是这样的db.GetCollection"Name") ->看源码重构了一个MongoCollection_1类
//将老版方法重构为db.GetCollection<Documnet>"Name");
//此方法其实将类名反射为集合 db.GetCollection<MyClass>"MyClass")
var collection = db.GetCollection<MyClass>);

//.net 3.5集合初始化特性 创建3个对象(2个MyClass,1个子类SubClass)
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.Savesquare);
collection.Savecircle);
collection.Savesub);

#region LINQ写法读取数据
//var superclass = from item in db.GetCollection<MyClass>"MyClass").Linq)
// where item.Corners > 1
// select item.Corners).ToList);

//var subclass = from item in db.GetCollection<SubClass>"MyClass").Linq)
// where item.Ratio > 1
// select item.Corners).ToList);
#endregion

//Lambda写法
//读取集合MyClass所有对象
Console.WriteLine"Count by LINQ on typed collection: {0}", collection.Linq).Countx => x.Corners > 1));
//读取集合为SubClass的对象由于SubClass被保存到MyClass集合中了,故结果为0)
Console.WriteLine"Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>).Linq).Countx => x.Corners > 1));
//Console.WriteLine"Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>).Countnew { Corners = Op.GreaterThan1) }));

//注:op 是Document的子类 虽然我们存的是其它对象,但所有的对象最终都是以Document类型存储的,
//也就意味检索数据也是Document方式查找或统计了,以下Lambda方式的统计就体现了
//在基类中大家可以看到都是countnew Document))方式及条件查询的,这里介绍完了,下面的代码就容易理解了
Console.WriteLine"Count on typed collection: {0}", collection.Countnew { Corners = Op.GreaterThan1) }));
var count
= collection.Countnew Document"Corners", Op.GreaterThan1)));
Console.WriteLine
"Count: {0}", count);
Console.ReadKey);
}
finally
{
mongo.Disconnect);
}
}

//var main = new MainClass);
//main.Setup);
//main.Run);

Console.ReadLine);
}

结果:

第三节 MongoDB下samus源码初探-风君子博客

再看mongo.exe中的记录:

 第三节 MongoDB下samus源码初探-风君子博客

三、LINQ的体现

第三节 MongoDB下samus源码初探-风君子博客第三节 MongoDB下samus源码初探-风君子博客View Code #region LINQ写法读取数据
var myCollection
= from item in db.GetCollection<MyClass>"MyClass").Linq)
where item.Corners > 1
select item).ToList);
foreach MyClass my in myCollection)
{
Console.WriteLinemy.Name
+":"+my.Corners.ToString));
}

var subCollection = from item in db.GetCollection<SubClass>"MyClass").Linq)
where item.Ratio > 1
select item).ToList);

foreach SubClass subClass in subCollection)
{
Console.WriteLinesubClass.Name
+":"+subClass.Ratio.ToString));
}

var superclass = from item in db.GetCollection<MyClass>"MyClass").Linq)
where item.Corners > 1
select item.Corners).ToList);

var subclass = from item in db.GetCollection<SubClass>"MyClass").Linq)
where item.Ratio > 1
select item.Corners).ToList);

#endregion

结果:

第三节 MongoDB下samus源码初探-风君子博客

以上就是LINQ的查找数据的方式,更多的方法还要研究一下源码的MongDB下的LINQ文件夹。

四、类关系分析

第三节 MongoDB下samus源码初探-风君子博客

以上四个类是MongoDB的基本业务类,从数据库连接,到实例,集合的CRUD操作。

我们在看一下配置映射类图:

第三节 MongoDB下samus源码初探-风君子博客

配置类组合的类比较多,具体还有哪些功能,在后面的学习过程在继续挖掘了。

好了这一节就到这里了,继续探讨复杂对象的储存。

转自:http://www.cnblogs.com/mecity/archive/2011/06/12/2079215.html