假设我想使用复合模式对我的域进行建模。模型(gist)非常简单。在专辑控制器中,我想对给定名称执行搜索:
public ActionResult Edit(String name) {
if(name == null) {
ViewBag.ViewRoot = true;
return View(this.albums.All());
}
var album = this.albums.Single(a => a.Name == name);
if(album != null) {
ViewBag.ViewRoot = false;
return View(album.Yield());
}
return View("404", (Object)("Album:" + name));
}
生成的(已过滤的)持久对象如下所示:
{
"_id" : "52ab1a6a7b88c91e5cfc39ff",
"ParentId" : null,
"Name" : "sample01",
"Items" : [{
"_t" : "Album",
"_id" : null,
"ParentId" : "52ab1a6a7b88c91e5cfc39ff",
"Name" : "sample02",
"Items" : [{
"_t" : "Album",
"_id" : null,
"ParentId" : null,
"Name" : "sample03",
"Items" : []
}]
}]
}
Mongodb不会立即执行递归搜索。但它允许您执行查询,因为您提供了明确的direction
(代码示例取自MongoDB + C# driver + query array of elements where each array element contains sub-document to query on):
Query.ElemMatch("Children", Query.And(Query.EQ("StatusId", 1), Query.EQ("Active", true),Query.LT("SubChild.ExpiresOn", DateTime.UtcNow)));
我一直在考虑两种解决方案(可能会保留composite
):
您对此有何看法?
答案 0 :(得分:1)
在mongodb
中,有一些用于建模树结构的设计模式是: