public async Task<ActionResult> Index() { var blogContext = new BlogContext(); // XXX WORK HERE // find the most recent 10 posts and order them // from newest to oldest var recentPosts = blogContext.Posts.Find(m => true).SortBy(m => m.CreatedAtUtc).ToListAsync().Result.Take(10).ToList(); var model = new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); var recentPosts = await blogContext.Posts.Find(x => true) .SortByDescending(x => x.CreatedAtUtc) .Limit(10) .ToListAsync(); var model = new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); // Find the most recent 10 posts and order them from newest to oldest. var recentPosts = await blogContext.Posts.Find<Post>(Builders<Post>.Filter.Empty) .SortByDescending(d => d.CreatedAtUtc) .Limit(10) .ToListAsync(); var model = new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); // XXX WORK HERE // find the most recent 10 posts and order them // from newest to oldest var sort = Builders<Post>.Sort.Descending(p => p.CreatedAtUtc); var recentPosts = await blogContext.Posts.Find(new BsonDocument()).Sort(sort).Limit(10).ToListAsync(); var model=new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); // XXX WORK HERE // find the most recent 10 posts and order them // from newest to oldest var recentPosts = await blogContext.Posts.Find(new BsonDocument()).ToListAsync(); recentPosts = recentPosts.OrderByDescending(post => post.CreatedAtUtc).Take(10).ToList(); var model = new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); // XXX WORK HERE // find the most recent 10 posts and order them // from newest to oldest IMongoCollection<Post> postsCollection = getPostsCollection(); List<Post> recentPosts = await postsCollection.Find(new BsonDocument()) .Sort(Builders<Post>.Sort.Descending("CreatedAtUtc")) .Limit(10) .ToListAsync(); var model = new IndexModel { RecentPosts = recentPosts }; return View(model); }
public async Task<ActionResult> Index() { var blogContext = new BlogContext(); var recentPosts = await blogContext.Posts.Find(x => true) .SortByDescending(x => x.CreatedAtUtc) .Limit(10) .ToListAsync(); var tags = await blogContext.Posts.Aggregate() .Project(x => new { _id = x.Id, Tags = x.Tags }) .Unwind(x => x.Tags) .Group<TagProjection>("{ _id: '$Tags', Count: { $sum: 1 } }") .ToListAsync(); var model = new IndexModel { RecentPosts = recentPosts, Tags = tags }; return View(model); }
public async Task<ActionResult> Index() { // XXX WORK HERE // find the most recent 10 posts and order them // from newest to oldest var recentPosts = await _blogContext.Posts.Find(new BsonDocument()) .SortByDescending(f=>f.CreatedAtUtc) .Limit(10) .ToListAsync(); var tags = await _blogContext.Posts.Aggregate() .Project(d => new {_id = d.Id, Tags = d.Tags}) .Unwind(f => f.Tags) .Group<TagProjection>("{_id:'$Tags',Count:{$sum:1}}") .ToListAsync(); var orderedTags = tags.OrderByDescending(ks => ks.Count).Take(10).ToList(); var model = new IndexModel { RecentPosts = recentPosts ?? new List<Post>(), Tags = orderedTags }; return View(model); }