简体   繁体   English

如何在 ArangoDB 中查找数组中具有特定日期(范围)的文档?

[英]How to find documents in ArangoDB which have a certain date (range) in an array?

I am trying to find all documents within a collection that have events within a certain date range.我正在尝试查找集合中具有特定日期范围内事件的所有文档。

Simplified the document structure looks like this:简化后的文档结构如下所示:

{
    "name": "Example document",
    "other": "property",
    "date_events": [
        {
            "start": "1963-10-12T00:00:00.000Z",
            "end": "1963-10-13T12:00:00.000Z"
        },
        {
            "start": "1970-04-20T00:00:00.000Z",
            "end": "1970-04-20T12:00:00.000Z"
        }
    ]
}

There are n documents, which all have the property date_events , which is an array of objects, containing n events, each event has start and end .n 个文档,它们都有属性date_events ,它是一个对象数组,包含n 个事件,每个事件都有startend

Now I want to find all documents which have events within a certain date range, eg.现在我想查找在某个日期范围内有事件的所有文档,例如。 between 1970-04-10 and 1970-04-28 : 1970-04-101970-04-28之间:

FOR doc IN api_documents
    FILTER (doc.date_events[*].start ANY >= "1970-04-10T00:00:00.000Z" &&
        doc.date_events[*].end ANY <= "1970-04-28T12:00:00.000Z")
    RETURN { _key: doc._key, _id: doc._id, events: doc.date_events[*] }

The problem is, that the above query is incorrect and the result contains also documents that have no matching events whatsoever, eg.问题是,上面的查询不正确,结果还包含没有匹配事件的文档,例如。 this one:这个:

{
    "name": "False positive",
    "other": "property",
    "date_events": [
        {
            "start": "1966-02-24T00:00:00.000Z",
            "end": "1966-02-24T12:00:00.000Z"
        },
        {
            "start": "1979-11-26T00:00:00.000Z",
            "end": "1979-11-30T12:00:00.000Z"
        },
        {
            "start": "1980-01-31T00:00:00.000Z",
            "end": "1980-01-31T12:00:00.000Z"
        }
    ]
}

I can get it to work when only filtering for one property, eg.当只过滤一个属性时,我可以让它工作,例如。 start . start But as soon as both start and end is in the query, all the queries that I was able to come up with, either produce zero results or false positives, which do not match the input date range.但是一旦startend都在查询中,我能够提出的所有查询要么产生零结果,要么产生与输入日期范围不匹配的误报。

Is the usage of the ANY operator incorrectly here when looking for two properties or what am I doing wrong?查找两个属性时,此处是否错误地使用了ANY运算符,或者我做错了什么?

Thank you.谢谢你。

Your current query returns every document that includes date_events that fit either criterion.您当前的查询返回包含符合任一标准的date_events的每个文档。

To make sure both criteria are matched by a single date_event , you can filter date_events in a sub-query and then filter for non empty results, something like this (untested, but should get you started):为确保两个条件都与单个date_event匹配,您可以在子查询中过滤date_events ,然后过滤非空结果,如下所示(未经测试,但应该让您入门):

FOR doc IN api_documents
    LET matches = (
        FOR de IN doc.date_events
            FILTER de.start >= "1970-04-10T00:00:00.000Z" &&
                   de.end <= "1970-04-28T12:00:00.000Z"
            RETURN 1
    )
    FILTER LENGTH(matches) > 0
    RETURN { _key: doc._key, _id: doc._id, events: doc.date_events }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM