2017-03-09 17 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); 

2つの日付の間の保存されたデータを取り込むためのクエリ文字列:

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); 
関連する問題