2013-05-04 68 views
0

嗨,我剛剛開始建立一個圍繞Mongo DB c#的cms。Mongo DB嵌套的CRUD c#

我有一個看起來像這樣爲了簡單起見這裏刪除了一些領域的基本文件......

{ "_id" : ObjectId("518438c35ea2e913ec41c138"), "Content" : "Some html content here", "Title" : "Robs Article", "Author" : "Rob Paddock", "DatePosted" : ISODate("0001-01-01T00:00:00Z"), "ArticleStatus" : "Live" } 

要調用我有以下代碼

public IEnumerable<Article> GetArticleDetails(int limit, int skip) 
    { 
     var articlesCursor = this.MongoConnectionHandler.MongoCollection.FindAllAs<Article>() 
      .SetSortOrder(SortBy<Article>.Descending(a => a.Title)) 
      .SetLimit(limit) 
      .SetSkip(skip) 
      .SetFields(Fields<Article>.Include(a => a.Id, a => a.Title, a => a.Author)); 
     return articlesCursor; 
    } 

要創建一個新文檔我有

public virtual void Create(T entity) 
    { 
     //// Save the entity with safe mode (WriteConcern.Acknowledged) 
     var result = this.MongoConnectionHandler.MongoCollection.Save(
      entity, 
      new MongoInsertOptions 
      { 
       WriteConcern = WriteConcern.Acknowledged 
      }); 

     if (!result.Ok) 
     { 
      //// Something went wrong 
     } 
    } 

我的問題是我將如何改變上述允許「內容」以成爲一個列表,因爲我可能想在頁面上有多個內容塊。

回答

0
public class Article 
{ 
    public BsonObjectId Id { get; set; } 
    public List<string> Content { get; set; } 
    public string Title { get; set; } 
    public string Author { get; set; } 
    public string DatePosted { get; set; } 
    public string ArticleStatus { get; set; } 

    public void AddContent(string c) 
    { 
     if (Content == null) 
     { 
      Content = new List<string>(); 
     } 
     Content.Add(c); 
    } 
} 

...

 var article = new Article { Title = "Robs Article", Author = "Rob Paddock", DatePosted="1/1/1980", ArticleStatus="Live" }; 
     article.AddContent("Some html content here"); 
     article.AddContent("NYT Featured"); 
     article.AddContent("Recommended for gourmets"); 

     var articleCollection = database.GetCollection<Article>("articles"); 
     articleCollection.Insert(article); 

...

> db.articles.find() 
{ "_id" : ObjectId("5185358ee153db0e0c6fa36a"), "Content" : [ "Some html content here", "NYT Featured", "Recommended for gourmets" ], "Title" : "Robs Article", "Author" : "Rob Paddock", "DatePosted" : 
"1/1/1980", "ArticleStatus" : "Live" } 
+0

由於工作的罰款。 – 2013-05-08 11:08:50