2017-03-09 63 views

回答

0

我沒有Java代碼,但是您必須將您的集合設置爲在DateTime字符串上使用Range索引,然後您才能查詢某個時間範圍內的日期。在.NET代碼是建立索引的範圍是:

DocumentCollection collection = new DocumentCollection { Id = "orders" }; 
collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }); 
await client.CreateDocumentCollectionAsync("/dbs/orderdb", collection); 

https://docs.microsoft.com/en-us/azure/documentdb/documentdb-working-with-dates#indexing-datetimes-for-range-queries和搜索範圍https://docs.microsoft.com/en-us/azure/documentdb/documentdb-indexing-policies此頁面上。

0

僅使用Azure DocumentDB SDK for Java通過_ts屬性查詢文檔。文檔_ts屬性如下。

_ts:這是系統生成的屬性。它指定資源的最後更新時間戳。該值是一個時間戳。

這是我的示例代碼如下。其次是_ts單位。

// Initialize a DocumentDb client. 
String serviceEndpoint = "https://<your-documentdb-name>.documents.azure.com:443/"; 
String masterKey = "<your-documentdb-key>"; 
DocumentClient client = new DocumentClient(serviceEndpoint, masterKey, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session); 
// Get a collection link via collection id. 
String collId = "<collection-Id>"; 
String query = String.format("SELECT * from ROOT r WHERE r.id = '%s'", dcId); 
FeedOptions options = null; 
List<DocumentCollection> dcs = client.queryCollections(dbLink, query, options).getQueryIterable().toList(); 
String collLink = dcs.size() >0 ? dcs.get(0).getSelfLink() : null; 
// Generate a query string, see below. 
....... 
client.queryDocuments(collection.getSelfLink(), query, null); 

查詢字符串用於讀取存儲在特定的日期數據:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
String dateStr = "2017-03-01"; 
long timestamp = sdf.parse(dateStr).getTime()/1000; 
String query = String.format("select * from c where c._ts = %d", timestamp); 

查詢字符串爲取存放兩個日期之間的數據:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
String startDateStr = "2017-03-01"; 
String endDateStr = "2017-03-31"; 
long tsStart = sdf.parse(startDateStr).getTime()/1000; 
long tsEnd = sdf.parse(endDateStr).getTime()/1000; 
String query = String.format("select * from c where c._ts >= %d and c._ts <= %d", tsStart, tsEnd); 
相關問題