You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/working-with-dates.md
+28-23Lines changed: 28 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,11 @@ ms.service: cosmos-db
5
5
author: SnehaGunda
6
6
ms.author: sngun
7
7
ms.topic: conceptual
8
-
ms.date: 09/25/2019
8
+
ms.date: 03/03/2020
9
9
---
10
10
# Working with Dates in Azure Cosmos DB
11
-
Azure Cosmos DB delivers schema flexibility and rich indexing via a native [JSON](https://www.json.org) data model. All Azure Cosmos DB resources including databases, containers, documents, and stored procedures are modeled and stored as JSON documents. As a requirement for being portable, JSON (and Azure Cosmos DB) supports only a small set of basic types: String, Number, Boolean, Array, Object, and Null. However, JSON is flexible and allow developers and frameworks to represent more complex types using these primitives and composing them as objects or arrays.
11
+
12
+
Azure Cosmos DB delivers schema flexibility and rich indexing via a native [JSON](https://www.json.org) data model. All Azure Cosmos DB resources including databases, containers, documents, and stored procedures are modeled and stored as JSON documents. As a requirement for being portable, JSON (and Azure Cosmos DB) supports only a small set of basic types: String, Number, Boolean, Array, Object, and Null. However, JSON is flexible and allow developers and frameworks to represent more complex types using these primitives and composing them as objects or arrays.
12
13
13
14
In addition to the basic types, many applications need the DateTime type to represent dates and timestamps. This article describes how developers can store, retrieve, and query dates in Azure Cosmos DB using the .NET SDK.
14
15
@@ -18,13 +19,14 @@ Azure Cosmos DB supports JSON types such as - string, number, boolean, null, arr
18
19
19
20
Most applications can use the default string representation for DateTime for the following reasons:
20
21
21
-
* Strings can be compared, and the relative ordering of the DateTime values is preserved when they are transformed to strings.
22
+
* Strings can be compared, and the relative ordering of the DateTime values is preserved when they are transformed to strings.
22
23
* This approach doesn't require any custom code or attributes for JSON conversion.
23
24
* The dates as stored in JSON are human readable.
24
25
* This approach can take advantage of Azure Cosmos DB's index for fast query performance.
25
26
26
27
For example, the following snippet stores an `Order` object containing two DateTime properties - `ShipDate` and `OrderDate` as a document using the .NET SDK:
27
28
29
+
```csharp
28
30
publicclassOrder
29
31
{
30
32
[JsonProperty(PropertyName="id")]
@@ -34,50 +36,53 @@ For example, the following snippet stores an `Order` object containing two DateT
This document is stored in Azure Cosmos DB as follows:
47
50
51
+
```json
48
52
{
49
53
"id": "09152014101",
50
54
"OrderDate": "2014-09-15T23:14:25.7251173Z",
51
55
"ShipDate": "2014-09-30T23:14:25.7251173Z",
52
56
"Total": 113.39
53
57
}
54
-
58
+
```
55
59
56
-
Alternatively, you can store DateTimes as Unix timestamps, that is, as a number representing the number of elapsed seconds since January 1, 1970. Azure Cosmos DB's internal Timestamp (`_ts`) property follows this approach. You can use the [UnixDateTimeConverter](https://msdn.microsoft.com/library/azure/microsoft.azure.documents.unixdatetimeconverter.aspx) class to serialize DateTimes as numbers.
60
+
Alternatively, you can store DateTimes as Unix timestamps, that is, as a number representing the number of elapsed seconds since January 1, 1970. Azure Cosmos DB's internal Timestamp (`_ts`) property follows this approach. You can use the [UnixDateTimeConverter](https://msdn.microsoft.com/library/azure/microsoft.azure.documents.unixdatetimeconverter.aspx) class to serialize DateTimes as numbers.
57
61
58
-
## Indexing DateTimes for range queries
59
-
Range queries are common with DateTime values. For example, if you need to find all orders created since yesterday, or find all orders shipped in the last five minutes, you need to perform range queries. To execute these queries efficiently, you must configure your collection for Range indexing on strings.
62
+
## Querying DateTimes in LINQ
60
63
61
-
DocumentCollection collection = new DocumentCollection { Id = "orders" };
62
-
collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
The SQL .NET SDK automatically supports querying data stored in Azure Cosmos DB via LINQ. For example, the following snippet shows a LINQ query that filters orders that were shipped in the last three days:
64
65
65
-
You can learn more about how to configure indexing policies at [Azure Cosmos DB Indexing Policies](index-policy.md).
The SQL .NET SDK automatically supports querying data stored in Azure Cosmos DB via LINQ. For example, the following snippet shows a LINQ query that filters orders that were shipped in the last three days.
70
+
Translated to the following SQL statement and executed on Azure Cosmos DB:
0 commit comments