Очень старый кусок кода (~2012-13 гг.), но вдруг пригодится когда-нибудь, память освежить.
var match = new BsonDocument
{ {
"$match", new BsonDocument
{
{
"Date", new BsonDocument
{ { "$lte", DateTime.Now } }
}
}
} };
var group = new BsonDocument
{ {
"$group", new BsonDocument
{
{ "_id","$Blog._id" },
{
"PublishDate", new BsonDocument
{ { "$max", "$Date" } }
}
}
} };
var project = new BsonDocument
{ {
"$project", new BsonDocument
{
{ "Blog._id", "$_id" },
{ "PublishDate", "$PublishDate" }
}
} };
var sort = new BsonDocument { { "$sort", new BsonDocument { { "PublishDate", -1 } } } };
var skip = new BsonDocument { { "$skip", (page - 1) * count } };
var limit = new BsonDocument { { "$limit", count } };
var pipeline = new[] { match, group, sort, project, skip, limit };
var lastPostsInBlogs = this.mongoContext.Posts
.Aggregate(pipeline)
.ResultDocuments.Select(x => BsonSerializer.Deserialize(x))
.ToList();
return
lastPostsInBlogs.Join(
this.mongoContext.Blogs.AsQueryable()
.Where(condition),
pb => pb.Blog.Id,
b => b.Id,
(pb, b) =>
new BlogItem()
{
Id = b.StrId,
Alias = b.Alias,
Title = b.Title,
Description = b.Info,
CoverFile = b.CoverFile ?? DomainAppSettings.DefaultPreview,
CanPost = b.Owner.AccountId == visitorId || (b.IsCommon && b.Members.Contains(visitorId)),
LastUpdated = b.LastUpdated,
LastPosts = this.mongoContext.Posts
.AsQueryable()
.Where(p => p.Blog.Id == b.Id && p.PublishDate p.PublishDate)
.Take(3)
.Select(p => new PostLine()
{
Id = p.StrId,
Number = p.Number,
BlogAlias = p.Blog.Alias,
Title = p.Title,
Date = p.Date
})
}).ToArray();