Date Between Query in Cosmos DB

我的梦境 提交于 2020-01-07 05:46:09

问题


I am in the building a simple event store in Cosmos DB that has documents that are structured something like this:

{
    "id": "e4c2bbd0-2885-4fb5-bcca-90436f79f155",
    "entityType": "contact",
    "history": [
        {
           "startDate": 1504656000,
           "endDate": 1504656000,
           "Name": "John"
        },
        {
           "startDate": 1504828800,
           "endDate": 1504828800,
           "Name": "Jon"
        }
    ]
}

This might not bet the most efficient way to store it but this is what I am starting with. But I want to be able to query all contact documents out of the db for a certain period of time. The startDate and endDate represent the time the record was valid. The history currently contains the entire history of the record which probably could be improved.

I have tried creating a query like this:

SELECT c.entityType, c.id,history.Name, history.startDate  FROM c
JOIN history in c.history
where
    c.entityType = "contact" AND
    (history.StartDate <= 1504656001
        AND history.EndDate  >=  1504656001)

This query should return the state of the contact for 9/7/2017 but instead it is returning every one of the history. I have played with several options but I am not sure what I am missing.

I have also tried setting the index (maybe that is the issue?) So I have included the indexing policy here:

{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
    {
        "path": "/*",
        "indexes": [
            {
                "kind": "Range",
                "dataType": "String",
                "precision": -1
            },
            {
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            }
        ]
    }
],
"excludedPaths": []
}

What am I missing? Is the index correct? Is my query correct for a date between query?


回答1:


You have two issues. One is addressed by Matias in comment.

Second, your condition is history.StartDate <= 1504656001 AND history.EndDate >= 1504656001.

play with the range for e.g. history.StartDate >= 1504656001 AND history.EndDate <= 1504656111.



来源:https://stackoverflow.com/questions/46084333/date-between-query-in-cosmos-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!