Sorting Azure DocumentDB的排序
我想使用DocumentDB存储大约200000个相同类型的文档。每个文档都有一个整数id字段,我希望以相反的顺序(首先是最高id)检索它们 所以最近我发现DocumentDB没有排序功能(另请参见)。也许最好换一个不同的数据库(比如RavenDB),但是时间紧迫,我想避免切换到另一个数据库的成本 问题是: 我一直在考虑在客户端实现我自己的文档排序索引(ASP Web API 2)。我在考虑创建一个键(id)和值(document.selflink)的分类列表。然后,我可以创建一个Getter,其中包含count、offset参数和一个谓词来过滤文档。下面我添加了一个快速示例 我只是觉得这是个坏主意;要么速度慢,耗费太多资源,要么换一种方式做得更好。所以我对实施建议持开放态度Sorting Azure DocumentDB的排序,sorting,azure-cosmosdb,Sorting,Azure Cosmosdb,我想使用DocumentDB存储大约200000个相同类型的文档。每个文档都有一个整数id字段,我希望以相反的顺序(首先是最高id)检索它们 所以最近我发现DocumentDB没有排序功能(另请参见)。也许最好换一个不同的数据库(比如RavenDB),但是时间紧迫,我想避免切换到另一个数据库的成本 问题是: 我一直在考虑在客户端实现我自己的文档排序索引(ASP Web API 2)。我在考虑创建一个键(id)和值(document.selflink)的分类列表。然后,我可以创建一个Getter,
public class SortableDocumentDbRepository
{
private SortedList _sorted = new SortedList();
private readonly string _sortedPropertyName;
private DocumentCollection ReadOrCreateCollection(string databaseLink) {
DocumentCollection col = base.ReadOrCreateCollection(databaseLink);
var docs = Client.CreateDocumentQuery(Collection.DocumentsLink)
.AsEnumerable();
lock (_sorted.SyncRoot) {
foreach (Document doc in docs) {
var propVal = doc.GetPropertyValue<string>(_sortedPropertyName);
if (propVal != null) {
_sorted.Add(propVal, doc.SelfLink);
}
}
}
return col;
}
public List<T> GetItems<T>(int count, int offset, Expression<Func<T, bool>> predicate) {
List<T> result = new List<T>();
lock (_sorted.SyncRoot) {
var values = _sorted.GetValueList();
for (int i = offset; i < _sorted.Count; i++) {
var queryable = predicate != null ?
Client.CreateDocumentQuery<T>(values[i].ToString()).Where(predicate) :
Client.CreateDocumentQuery<T>(values[i].ToString());
T item = queryable.AsEnumerable().FirstOrDefault();
if (item == null || item.Equals(default(T))) continue;
result.Add(item);
if (result.Count >= count) return result;
}
}
return result;
}
}
公共类SortableDocumentDbRepository
{
私有分类列表_sorted=新分类列表();
私有只读字符串_sortedPropertyName;
private DocumentCollection Reader或CreateCollection(字符串数据库链接){
DocumentCollection col=base.ReadOrCreateCollection(databaseLink);
var docs=Client.CreateDocumentQuery(Collection.DocumentsLink)
.AsEnumerable();
锁(_sorted.SyncRoot){
foreach(文档中的文档){
var propVal=doc.GetPropertyValue(_sortedPropertyName);
if(propVal!=null){
_已排序.添加(propVal,doc.SelfLink);
}
}
}
返回列;
}
公共列表GetItems(整数计数、整数偏移量、表达式谓词){
列表结果=新列表();
锁(_sorted.SyncRoot){
var values=_sorted.GetValueList();
对于(int i=偏移量;i<\u sorted.Count;i++){
var queryable=谓词!=null?
Client.CreateDocumentQuery(值[i].ToString())。其中(谓词):
CreateDocumentQuery(值[i].ToString());
T item=queryable.AsEnumerable().FirstOrDefault();
如果(item==null | | item.Equals(默认值(T)))继续;
结果.添加(项目);
如果(result.Count>=Count)返回结果;
}
}
返回结果;
}
}
正如您所提到的,order by不幸尚未实现
我觉得你的方法很合理
我看到您正在使用一个谓词来缩小查询结果集(为任何数据库提取200000条记录将是昂贵的)
由于看起来您是按id
订购的-您还可以查看在id
上设置范围索引,允许您在id
上执行范围查询(例如
),并进一步缩小查询结果集。默认情况下,文档上的\u ts
(timestamp)系统属性中还包含一个范围索引,在这种情况下可能会有所帮助
请参阅:如您所述,order by不幸尚未实施 我觉得你的方法很合理 我看到您正在使用一个谓词来缩小查询结果集(为任何数据库提取200000条记录将是昂贵的) 由于看起来您是按
id
订购的-您还可以查看在id
上设置范围索引,允许您在id
上执行范围查询(例如
),并进一步缩小查询结果集。默认情况下,文档上的\u ts
(timestamp)系统属性中还包含一个范围索引,在这种情况下可能会有所帮助
请参阅:Microsoft已实施排序:
示例:按c从c订单中选择*。_tsdescMicrosoft已实施排序:
示例:按c从c顺序选择*。_tsdesc我使用了_ts字段,并且能够根据cosmos db中的创建日期时间戳对记录进行排序。我使用了_ts字段,并且能够根据cosmos db中的创建日期时间戳对记录进行排序。已更新!感谢您的通知更新!谢谢你的注意