Skip to content

Commit 36387d7

Browse files
authored
Merge pull request #106340 from timsander1/master
update datetime doc
2 parents eb8521f + 9a53501 commit 36387d7

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

articles/cosmos-db/working-with-dates.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ ms.service: cosmos-db
55
author: SnehaGunda
66
ms.author: sngun
77
ms.topic: conceptual
8-
ms.date: 09/25/2019
8+
ms.date: 03/03/2020
99
---
1010
# 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.
1213

1314
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.
1415

@@ -18,13 +19,14 @@ Azure Cosmos DB supports JSON types such as - string, number, boolean, null, arr
1819

1920
Most applications can use the default string representation for DateTime for the following reasons:
2021

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.
2223
* This approach doesn't require any custom code or attributes for JSON conversion.
2324
* The dates as stored in JSON are human readable.
2425
* This approach can take advantage of Azure Cosmos DB's index for fast query performance.
2526

2627
For example, the following snippet stores an `Order` object containing two DateTime properties - `ShipDate` and `OrderDate` as a document using the .NET SDK:
2728

29+
```csharp
2830
public class Order
2931
{
3032
[JsonProperty(PropertyName="id")]
@@ -34,50 +36,53 @@ For example, the following snippet stores an `Order` object containing two DateT
3436
public double Total { get; set; }
3537
}
3638

37-
await client.CreateDocumentAsync("/dbs/orderdb/colls/orders",
38-
new Order
39-
{
39+
await container.CreateItemAsync(
40+
new Order
41+
{
4042
Id = "09152014101",
4143
OrderDate = DateTime.UtcNow.AddDays(-30),
4244
ShipDate = DateTime.UtcNow.AddDays(-14),
4345
Total = 113.39
4446
});
47+
```
4548

4649
This document is stored in Azure Cosmos DB as follows:
4750

51+
```json
4852
{
4953
"id": "09152014101",
5054
"OrderDate": "2014-09-15T23:14:25.7251173Z",
5155
"ShipDate": "2014-09-30T23:14:25.7251173Z",
5256
"Total": 113.39
5357
}
54-
58+
```
5559

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.
5761

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
6063

61-
DocumentCollection collection = new DocumentCollection { Id = "orders" };
62-
collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
63-
await client.CreateDocumentCollectionAsync("/dbs/orderdb", collection);
64+
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:
6465

65-
You can learn more about how to configure indexing policies at [Azure Cosmos DB Indexing Policies](index-policy.md).
66+
```csharp
67+
IQueryable<Order> orders = container.GetItemLinqQueryable<Order>(allowSynchronousQueryExecution: true).Where(o => o.ShipDate >= DateTime.UtcNow.AddDays(-3));
68+
```
6669

67-
## Querying DateTimes in LINQ
68-
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:
6971

70-
IQueryable<Order> orders = client.CreateDocumentQuery<Order>("/dbs/orderdb/colls/orders")
71-
.Where(o => o.ShipDate >= DateTime.UtcNow.AddDays(-3));
72-
73-
// Translated to the following SQL statement and executed on Azure Cosmos DB
72+
```sql
7473
SELECT * FROM root WHERE (root["ShipDate"] >= "2016-12-18T21:55:03.45569Z")
74+
```
7575

76-
You can learn more about Azure Cosmos DB's SQL query language and the LINQ provider at [Querying Cosmos DB](how-to-sql-query.md).
76+
You can learn more about Azure Cosmos DB's SQL query language and the LINQ provider at [Querying Cosmos DB in LINQ](sql-query-linq-to-sql.md).
7777

78-
In this article, we looked at how to store, index, and query DateTimes in Azure Cosmos DB.
78+
## Indexing DateTimes for range queries
79+
80+
Queries are common with DateTime values. To execute these queries efficiently, you must have an index defined on any properties in the query's filter.
81+
82+
You can learn more about how to configure indexing policies at [Azure Cosmos DB Indexing Policies](index-policy.md).
7983

8084
## Next Steps
85+
8186
* Download and run the [Code samples on GitHub](https://github.com/Azure/azure-documentdb-dotnet/tree/master/samples/code-samples)
82-
* Learn more about [SQL queries](how-to-sql-query.md)
87+
* Learn more about [SQL queries](sql-query-getting-started.md)
8388
* Learn more about [Azure Cosmos DB Indexing Policies](index-policy.md)

0 commit comments

Comments
 (0)