C#
如何从集合中获取插入last N
的文档mongo
?
问问题
6242 次
2 回答
8
我做了解决方案
SortByBuilder sbb = new SortByBuilder();
sbb.Descending("_id");
var allDocs = collection.FindAllAs<BsonDocument>().SetSortOrder(sbb).SetLimit(N);
于 2012-08-14T09:18:18.527 回答
1
The general pattern is using
docs = collection.find().sort({'_id' : -1}.limit(N)
By sorting on _id you will take into account that the standard object id is only increasing over time (unless implemented otherwise). Otherwise you need to sort on some timestamp field that you add/maintain within your code and application.
于 2012-08-13T13:14:31.517 回答