我在C#中编写了一些代码,它从一个集合中提取了1000个文档,我需要将这些文档从最新到最旧的文档中分类。我知道之前已经回答了类似的问题,但我找不到符合我代码的问题。这是代码:
DateTime dtLastDay = DateTime.Today.AddDays(-1);
var connectionString = ConfigurationManager.AppSettings["MongoAddress"];
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("Gnip");
var collection = database.GetCollection<DOC>("GnipUL");
collection.EnsureIndex(new IndexKeysBuilder().Ascending("Link"));
var query3 = Query.And(
Query<DOC>.EQ(e1 => e1.Type, iType),
Query<DOC>.GT (e1 => e1.dtPublish , dtLastDay )
);
//IMongoSortBy sort=Query<DOC>.EQ (e1 => e1.dtPublish ,-1);
var search = collection.Find(query3).SetLimit(1000);
public class DOC
{
public ObjectId Id { get; set; }
public int Type { get; set; }
public int taken { get; set; }
public string Link { get; set; }
public DateTime dtPublish { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Writer { get; set; }
public string Identity { get; set; }
public string Site { get; set; }
public string SiteLink { get; set; }
}
我知道我需要添加到行var search = collection.Find(query3).SetLimit(1000);
,。SetSort(),但我不知道在那里写什么,所以它将由dtPublish命令。
提前致谢, 陈。
答案 0 :(得分:0)
您可以使用SetSortOrder
作为:
var search = collection.Find(query3)
.SetSortOrder(SortBy.Descending("dtPublish"))
.SetLimit(1000);
或更强烈打字:
var search = collection.Find(query3)
.SetSortOrder(SortBy<DOC>.Descending(d => d.dtPublish))
.SetLimit(1000);