第四节 MongoDB复杂及变化对象的存储

本节探讨问题:

  • 复杂对象的存储
  • 存储的实体对象发生变化了

     既然要使MongoDB做业务驱动开发,那就要考虑到复杂的对象是否容易可以直接序列化存储呢?在后期的开发中,总会碰到对象的会增加或删除属性,那对象属性变动了,是否会对之前的存到MongoDB的数据反序列化会有影响呢?接下来我们就带着这些问题来简单测试一下,我也不知道是否会有问题。

一、复杂对象的存储

我们先来测试一些值类型的序列化问题,.net mvc中有两个方法用来将前台传回的数据绑定到对象上,TryUpdateModel(),UpdateModel(),我记得碰到日期,值类型若没赋值都会出错。而其它的序列化对数组,HashTable,日期等都要单独处理。我们来看一MongoDB是怎么处理的

  1.带参构造函数的类 

class Program
{
public class Employee
{
public string Name { set; get; }
public int Age { set; get; }
public bool Sex { set; get; }
public DateTime Birthday { set; get; }
public JobBase Job { set; get; }
public Hashtable HT { set; get; }
}

public class Company
{
public string Name { set; get; }
public DateTime CreateDate { set; get; }
public List<Employee> EmployeeCollection { set; get; }
public string[] Department { set; get; }
public double Rate { set; get; }
}

public class JobBase
{
public string Name { set; get; }
public Work Work { set; get; }
public void Action()
{
Console.WriteLine(
"我是" + this.Name);
}
}

public class Work
{
private string something;
public Work(string something)
{
this.something = something;
}

public void ToDo()
{
Console.WriteLine(
"我喜欢做" + something);
}
}


static void Main(string[] args)
{
Mongo mongo
= new Mongo("Server=127.0.0.1:27017");
mongo.Connect();

IMongoDatabase simple
= mongo["simple"];
var compCollection
= simple.GetCollection<Company>();

//Company company = new Company();
//company.CreateDate = DateTime.Now;
//company.Name = "C Company";
//company.Department = new string[] { "信息部", "财务部", "开发部" };

var empCollection
= simple.GetCollection<Employee>();

Employee employee1
= new Employee() { Name = "llj", Age = 24, Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee1.Job
= new JobBase() { Name = "IT" };
employee1.Job.Work
= new Work("IT杂志");
employee1.HT
= new Hashtable();
employee1.HT.Add(
"A1", "AA1");
Employee employee2
= new Employee() { Name = "cctv", Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee2.Job
= new JobBase() { Name = "TEST" };
employee2.Job.Work
= new Work("TEST测试");
employee2.HT
= new Hashtable();
employee2.HT.Add(
"B1", "BB1");
Employee employee3
= new Employee() { Name = "http", Age = 24, Sex = true };
employee3.Job
= new JobBase() { Name = "DEV" };
employee3.Job.Work
= new Work("DEV开发");
employee3.HT
= new Hashtable();
employee3.HT.Add(
"C3", "CC3");

//company.EmployeeCollection = new List<Employee>();
//company.EmployeeCollection.Add(employee1);
//company.EmployeeCollection.Add(employee2);
//company.EmployeeCollection.Add(employee3);
//compCollection.Save(company);

empCollection.Save(employee1);
empCollection.Save(employee2);
empCollection.Save(employee3);

Console.WriteLine(
"Count: {0}", empCollection.Linq().Count());
//选择一个对象查看
Employee employee=empCollection.FindOne(new Document{{"Name","llj"}});

//Company companyNew = compCollection.FindOne(new Document{{"Name","C Company"}});
//companyNew.EmployeeCollection[0].Job.Action();
//companyNew.EmployeeCollection[0].Job.Work.ToDo();
mongo.Disconnect();
mongo.Dispose();
Console.ReadLine();
}
}

程序出错了,原因是Work这个类是带参的构造函数的,也说你的对象是通过带参的构造函数创建的被保存Mongo中,再反射回来时会出错的。见下面

我们注掉Work类的所有代码,继续测试 db.Employee.drop()

2.值类型及日期的测试 

View Code
class Program
{
public class Employee
{
public string Name { set; get; }
public int Age { set; get; }
public bool Sex { set; get; }
public DateTime Birthday { set; get; }
public JobBase Job { set; get; }
public Hashtable HT { set; get; }
}

public class Company
{
public string Name { set; get; }
public DateTime CreateDate { set; get; }
public List<Employee> EmployeeCollection { set; get; }
public string[] Department { set; get; }
public double Rate { set; get; }
}

public class JobBase
{
public string Name { set; get; }
public void Action()
{
Console.WriteLine(
"我是" + this.Name);
}
}


static void Main(string[] args)
{
Mongo mongo
= new Mongo("Server=127.0.0.1:27017");
mongo.Connect();

IMongoDatabase simple
= mongo["simple"];
var compCollection
= simple.GetCollection<Company>();

//Company company = new Company();
//company.CreateDate = DateTime.Now;
//company.Name = "C Company";
//company.Department = new string[] { "信息部", "财务部", "开发部" };

var empCollection
= simple.GetCollection<Employee>();

Employee employee1
= new Employee() { Name = "llj", Age = 24, Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee1.Job
= new JobBase() { Name = "IT" };
employee1.HT
= new Hashtable();
employee1.HT.Add(
"A1", "AA1");
Employee employee2
= new Employee() { Name = "cctv", Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee2.Job
= new JobBase() { Name = "TEST" }; ;
employee2.HT
= new Hashtable();
employee2.HT.Add(
"B1", "BB1");
Employee employee3
= new Employee() { Name = "http", Age = 24, Sex = true };
employee3.Job
= new JobBase() { Name = "DEV" };
employee3.HT
= new Hashtable();
employee3.HT.Add(
"C3", "CC3");

//company.EmployeeCollection = new List<Employee>();
//company.EmployeeCollection.Add(employee1);
//company.EmployeeCollection.Add(employee2);
//company.EmployeeCollection.Add(employee3);
//compCollection.Save(company);

empCollection.Save(employee1);
empCollection.Save(employee2);
empCollection.Save(employee3);

Console.WriteLine(
"Count: {0}", empCollection.Linq().Count());
//选择一个对象查看
Employee employee=empCollection.FindOne(new Document{{"Name","llj"}});

//Company companyNew = compCollection.FindOne(new Document{{"Name","C Company"}});
//companyNew.EmployeeCollection[0].Job.Action();
mongo.Disconnect();
mongo.Dispose();
Console.ReadLine();
}
}

查看结果:

我们可以看到,未设置年龄,日期的字段,Mongo都会处理为0和一个指定日期,而HashTable也是可以被自动序列化进来的。

那再把看一下取出来的对象被还原成什么样了,

3.组合子对象,增加集合测试

View Code
class Program
{
public class Employee
{
public string Name { set; get; }
public int Age { set; get; }
public bool Sex { set; get; }
public DateTime Birthday { set; get; }
public JobBase Job { set; get; }
public Hashtable HT { set; get; }
}

public class Company
{
public string Name { set; get; }
public DateTime CreateDate { set; get; }
public List<Employee> EmployeeCollection { set; get; }
public string[] Department { set; get; }
public double Rate { set; get; }
}

public class JobBase
{
public string Name { set; get; }
public void Action()
{
Console.WriteLine(
"我是" + this.Name);
}
}


static void Main(string[] args)
{
Mongo mongo
= new Mongo("Server=127.0.0.1:27017");
mongo.Connect();

IMongoDatabase simple
= mongo["simple"];
var compCollection
= simple.GetCollection<Company>();

Company company
= new Company();
company.CreateDate
= DateTime.Now;
company.Name
= "C Company";
company.Department
= new string[] { "信息部", "财务部", "开发部" };

Employee employee1
= new Employee() { Name = "llj", Age = 24, Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee1.Job
= new JobBase() { Name = "IT" };
employee1.HT
= new Hashtable();
employee1.HT.Add(
"A1", "AA1");
Employee employee2
= new Employee() { Name = "cctv", Sex = true, Birthday = new DateTime(1984, 9, 9) };
employee2.Job
= new JobBase() { Name = "TEST" }; ;
employee2.HT
= new Hashtable();
employee2.HT.Add(
"B1", "BB1");
Employee employee3
= new Employee() { Name = "http", Age = 24, Sex = true };
employee3.Job
= new JobBase() { Name = "DEV" };
employee3.HT
= new Hashtable();
employee3.HT.Add(
"C3", "CC3");

company.EmployeeCollection
= new List<Employee>();
company.EmployeeCollection.Add(employee1);
company.EmployeeCollection.Add(employee2);
company.EmployeeCollection.Add(employee3);
compCollection.Save(company);

Company companyNew
= compCollection.FindOne(new Document { { "Name", "C Company" } });
companyNew.EmployeeCollection[
0].Job.Action();
mongo.Disconnect();
mongo.Dispose();
Console.ReadLine();
}
}

看一下结果:

可以看到List集合,字符串数组,组合类都已成功序列化保存到Mongo中了,那我们在还从Mongo把这个对象取出来

对象被成功的还原回来了,那我们修改现在的对象属性,去掉Company的Department属性,以及Employee的JobBase属性看看还原后的情况

4.增删属性再还原对象

View Code
public class Employee
{
public string Name { set; get; }
public int Age { set; get; }
public bool Sex { set; get; }
public DateTime Birthday { set; get; }
//public JobBase Job { set; get; }
public Hashtable HT { set; get; }
}

public class Company
{
public string Name { set; get; }
public DateTime CreateDate { set; get; }
public List<Employee> EmployeeCollection { set; get; }
//public string[] Department { set; get; }
public double Rate { set; get; }
}

查看结果,看看是否会影响对象成功还原

对象成功还原了,少掉那部分属性。那我们再将对象保存回去,看看Mongo会将那部分多出来的属性丢弃吗?为了表现明显,我们将Company的List属性也注释掉

执行以下代码

View Code
Company companyNew = compCollection.FindOne(new Document { { "Name", "C Company" } });
compCollection.Update(companyNew, companyNew);

查看结果Mongo中的结果:

 

读取后的对象被保存回去会将原先的多出的属性数据丢弃。

总结:经过测试,Mongo可存取复杂的对象(对象中不包含其它带参构造函数,影响到还原),包括数组,泛型集合,哈希表,值类型等等。非空字段Mongo会自动匹配一些特定的值。但是当对象本身发生变化了,比如删除属性后,读取后再更新回去,会导致原先的多出的属性丢失,因此开发中要考虑到对象变了之后,之前的数据是否要保留下来,做好备份。Mongo要求存储的对象要尽量相对稳定或者结构相对简单,否则前期的业务对象设计要花大量时间了。

posted on 2011-06-14 00:16  小城岁月  阅读(10338)  评论(3编辑  收藏  举报

导航

面朝大海,春暖花开!