我想存储名为" Wsp"在一个文档和一个名为" Sit"在另一个。我读了http://docs.mongodb.org/manual/core/data-modeling-introduction/所以我创建了这样的结构:
public class Wsp{
public ObjectId Id{get; set;}
public Guid WspId{get; set;}
public List<Sit> SitList{get; set;}
public string Name {get; set;}
public ......... {get; set;}
}
public class Sit{
public ObjectId Id {get; set;}
public Guid WspId; //Id of a parent Wsp object
public ......... {get; set;}
}
//disabling Wsp from saving SitList into document (only Name, WspID and other..)
BsonClassMap.RegisterClassMap<Wsp>(map =>
{
map.MapProperty(p => p.Name);
map.MapProperty(p => p.WspId);
...
});
现在我要创建GetWsp(Guid wspId)
和AddWsp(Wsp wsp)
,它会自动将List of Sit映射到Wsp。例如:
public Wsp GetFullWsp(Guid id)
{
var wsp = wspCollection.AsQueryable().FirstOrDefault(w => w.WspId == id);
//no code for List<Sit> !!
//wsp will have List<Sit> automatic filled from "SitCollection" based on WspId
}
如何配置此类查询?我可以以某种方式使用BsonClassMap.RegisterClassMap
吗?
这是一个很好的方法吗?我读到了MongoDBRef
,但有人告诉我,使用它会降低性能,性能对我来说是最重要的。
答案 0 :(得分:0)
假设你已经有了一个sitCollection:
public Wsp GetFullWsp(Guid id)
{
var wsp = wspCollection.AsQueryable().FirstOrDefault(w => w.WspId == id);
var sits = sitCollection.AsQueryable().Where(sit => sit.WspId == id);
wsp.SitList = new List<Sit>(sits);
}
但是,我想你可能想要考虑你的模型。我看到3个选项:
我会选择第一个,但这取决于您的具体需求。