2016-10-01 4 views
0

부모 문서의 자식 업데이트에 대한 몇 가지 질문을 찾았습니다. 단, 부모/자식 트리가 얼마나 멀리 떨어져 있는지 이미 알고 있어야합니다. 나는 전체 스레드를 제출하지 않고 부모 스레드에 대한 새로운 의견을 보내 MongoDB를의 업데이트 기능을 사용할 수 있어야합니다Mongodb c-n 중첩 된 주석 체인의 주석 업데이트

public class ParentThread 
{ 
    public string id { get; set; } 
    public string title { get; set; } 
    public string body { get; set; } 
    public List<Comment> Comments { get; set; } 
} 

public class Comment 
{ 
    public string id { get; set; } 
    public string body { get; set; } 
    public List<Comment> Comments { get; set; } 
} 

: 여기 내 모델입니다. 이것은 여러 사용자가 동시에 스레드를 추가하고 데이터베이스가이를 덮어 쓰는 문제를 피하기위한 것입니다. 문제는 스레드에 사용자의 설명을 추가하기 위해 나무까지 얼마나 멀리 mongodb로 지정해야할지 모르겠다는 것입니다.

추가 할 대상 주석을 찾기 위해 문서를 쿼리하고 트리를 탐색하는 방법을 알아 냈습니다.하지만이를 Update.Push()의 첫 번째 매개 변수로 전달하는 방법을 파악하는 데 문제가 있습니다. 방법. 어떤 아이디어?

답변

0

트리 구조를 모델링하는 몇 가지 권장 방법이 있습니다. 공식 문서에서 parent references을 살펴보십시오. 이것은 당신의 나무를 liniarize 것입니다. Parent References 패턴은 문서의 각 트리 노드를 저장합니다. 트리 노드 외에도 문서에는 노드 부모의 ID가 저장됩니다. 아래 나의 제안 :

// item is the base, comment is a thread comment, reply is a comment to a comment 
public enum ItemType { Item, Thread, Comment, Reply } 

public class Item { 
    [BsonId] public string Id { get; set; } 
    [BsonElement("body")] public string Body { get; set; } 
    [BsonRepresentation(MongoDB.Bson.BsonType.String)] 
    [BsonElement("type")] public virtual ItemType Type { get { return ItemType.Item; } } 
    [BsonDefaultValue(null)] 
    [BsonElement("parent")] public string ParentId { get; set; } 
    [BsonDefaultValue(null)] 
    [BsonElement("title")] public string Title { get; set; } 
    public override string ToString() { return String.Format("{0};{1};{2};{3};{4}", Id, Type, ParentId, Body, Title); } 
} 

public class Thread : Item { public override ItemType Type { get { return ItemType.Thread; } } } 

public class Comment : Item { public override ItemType Type { get { return ItemType.Comment; } } } 

public class Reply : Item { public override ItemType Type { get { return ItemType.Reply; } } } 

항목을 찾을 수있는 방법 드라이버 버전 2.3 :

IMongoCollection<item> col = ... 
// create index for parent column 
await col.Indexes.CreateOneAsync(Builders<Item>.IndexKeys.Ascending(x => x.ParentId)); 
var root = await (await col.FindAsync(fdb.Eq(x => x.ParentId, null))).SingleOrDefaultAsync(); 
var rootComments = await (await col.FindAsync(fdb.Eq(x => x.ParentId, root.Id))).ToListAsync(); 
// same thing for queries for replies to comments 

주요 장점은 삽입입니다. 삽입하고자하는 것을 부모님 만 알면됩니다. 더 이상 내포 된 발견 문제가 없습니다.