我正在尝试进行不同的排序算法,我通过set sort调用set。 这就是我想要的,我们有"帖子"收集:" Bob"有4个帖子和#34; Jamie"有3个帖子。
问题
在mongo中,我尝试对用户进行分组,然后对帖子进行排序,但没有$ second运算符或$ 3,我只有一个$ first运算符,这就是给我第一组帖子。无法获得第二,第三,第四套等等。
var group = new BsonDocument {
{ "_id", "$UserId" },
{ "_document", new BsonDocument { { "$first", "$$ROOT" } } }
};
// code ...
这只是给我第一份文件:
> [{ "UserId": "5b0154f3a614fbbb8a10fad0", "Title": "Bob Post 4", "DateTime": 2018-05-20 13:58:59.130, "Status": 0},
{ "UserId": "5b0154f3a614fbbb8a10fad1", "Title": "Jamie Post 3", "DateTime": 2018-05-20 13:57:59.130, "Status": 0}]
预期
下面的请求和响应显示我的set by set sort示例预期结果。按用户对帖子进行分组,并按日期时间对最新文档进行排序。每个带有偏移和限制的服务器的新请求我都希望按用户分组并按日期时间排序的最新文档。
1。请求和响应(获取用户的最新帖子):
> getPosts(offset: 0, limit: 2)
> [{ "UserId": "5b0154f3a614fbbb8a10fad0", "Title": "Bob Post 4", "DateTime": 2018-05-20 13:58:59.130, "Status": 0},
{ "UserId": "5b0154f3a614fbbb8a10fad1", "Title": "Jamie Post 3", "DateTime": 2018-05-20 13:57:59.130, "Status": 0}]
2。请求和响应(获取用户的最新帖子之前):
> getPosts(offset: 2, limit: 2)
> [{ "UserId": "5b0154f3a614fbbb8a10fad0", "Title": "Bob Post 3", "DateTime": 2018-05-20 12:58:59.130, "Status": 0},
{ "UserId": "5b0154f3a614fbbb8a10fad1", "Title": "Jamie Post 2", "DateTime": 2018-05-20 11:57:59.130, "Status": 0}]
3。请求和响应(在用户的最新帖子之前获取):
> getPosts(offset: 4, limit: 2)
> [{ "UserId": "5b0154f3a614fbbb8a10fad0", "Title": "Bob Post 2", "DateTime": 2018-05-20 12:58:59.130, "Status": 0},
{ "UserId": "5b0154f3a614fbbb8a10fad1", "Title": "Jamie Post 1", "DateTime": 2018-05-19 11:57:59.130, "Status": 0}]
收集后(文件):
UserId: 5b0154f3a614fbbb8a10fad0
Title: Bob Post 1
DateTime: 2018-05-20 13:58:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad0
Title: Bob Post 2
DateTime: 2018-05-20 12:58:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad1
Title: Jamie Post 1
DateTime: 2018-05-20 13:57:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad1
Title: Jamie Post 2
DateTime: 2018-05-20 11:57:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad0
Title: Bob Post 3
DateTime: 2018-05-20 12:58:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad1
Title: Jamie Post 3
DateTime: 2018-05-19 11:57:59.130
Status: 0
UserId: 5b0154f3a614fbbb8a10fad0
Title: Bob Post 4
DateTime: 2018-05-18 12:58:59.130
Status: 0
代码:
public virtual async Task<IEnumerable<Post>> PagingAsync(int offset, int limit, double lat, double lon, double maxDistanceMeters, double minDistanceMeters, string city, string category, string requestingUserId)
{
var col = _context._database.GetCollection<BsonDocument>("post");
var dateSort = Builders<BsonDocument>.Sort.Descending(document => document["DateTime"]);
var addRadar = Builders<BsonDocument>.Filter.Eq(document => document["AddToRadar"], true);
var approved = Builders<BsonDocument>.Filter.Eq(document => document["Status"], PostStatus.Approved);
var cityEq = Builders<BsonDocument>.Filter.Eq(document => document["City"], city);
var notEqualRequestedUserId = Builders<BsonDocument>.Filter.Ne(document => document["UserId"], requestingUserId);
if (city == "") {
cityEq = Builders<BsonDocument>.Filter.Empty;
}
var categoryEq = Builders<BsonDocument>.Filter.Eq(document => document["CategoryId"], category);
var group = new BsonDocument
{
{ "_id", "$UserId" },
{ "_document", new BsonDocument { { "$first", "$$ROOT" } } }
};
ProjectionDefinition<BsonDocument> projection = new BsonDocument{{ "document", "$_document"}};
List<BsonDocument> result;
if (category == "")
categoryEq = Builders<BsonDocument>.Filter.Empty;
if (requestingUserId == "")
notEqualRequestedUserId = Builders<BsonDocument>.Filter.Empty;
result = await col.Aggregate()
.Match(approved)
.Match(addRadar)
.Match(categoryEq)
.Match(cityEq)
.Match(notEqualRequestedUserId)
.Sort(dateSort)
.Group(group)
.Project(projection)
.Skip(offset)
.Limit(limit)
.ToListAsync();
return result.Select(x => BsonSerializer.Deserialize<Post>(x["document"].AsBsonDocument)).ToList();
}